您的位置:首页 > Web前端 > JavaScript

javascript父子通信

2007-05-27 00:00 267 查看
<script language="javascript" defer="defer">
var parent = 
{
    //
    say:function ()
    {
        //
        alert("parent_say");
    },
    alert:function ()
    {
        //
        alert("parent");
        this.say();
    }
};
//


parent.addChild("child",
    {
        //
        say:function ()
        {
            //
            alert("child_say");
        },
        alert:function ()
        {
            //
            alert("child");
            this.parent.say();
        }
    }
);
//parent.child.alert();
//parent.alert();
var c = parent.child;
c.alert();
</script>



<script language="javascript">
Object.prototype.addChild=function(oName, obj)
{
    eval("var p=this."+oName+"= new Object()");
    this[oName]=obj;
    this[oName].parent=this;
}
</script>
效果演示:

[Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息