Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:添加onclick属性DOM_Javascript_Jquery_Onclick - Fatal编程技术网

Javascript:添加onclick属性DOM

Javascript:添加onclick属性DOM,javascript,jquery,onclick,Javascript,Jquery,Onclick,我尝试向现有的div元素添加一些侦听器 //Prepare div $(this.div).css("z-index","256"); $(this.div).attr("onmouseover",this.myname+".resize();"); $(this.div).attr("onmousedown",this.myname+".resize();"); $(this.div).attr("onmouseo

我尝试向现有的div元素添加一些侦听器

        //Prepare div
        $(this.div).css("z-index","256");
        $(this.div).attr("onmouseover",this.myname+".resize();");
        $(this.div).attr("onmousedown",this.myname+".resize();");
        $(this.div).attr("onmouseout","if("+this.myname+".sized)"+ this.myname+".resize();");
但在IE和Chrome中,当事件仍然存在时,它不会被炒鱿鱼 添加到元素属性中。 Firefox按预期工作

有人知道有什么问题吗


谢谢不要将事件设置为字符串。
相反,您应该使用jQuery的
bind
方法:

var me = this;    //Inside the handlers, this is the element.

$(this.div).bind('mouseenter mousedown mouseleave',  function() { 
    me.resize(); 
});

不要将事件设置为字符串。
相反,您应该使用jQuery的
bind
方法:

var me = this;    //Inside the handlers, this is the element.

$(this.div).bind('mouseenter mousedown mouseleave',  function() { 
    me.resize(); 
});

您应该真正使用jquery自己的evenhandling(
.hover()
)而不是添加到属性中。您应该真正使用jquery自己的evenhandling(
.hover()
)而不是添加到属性中。@Mereep-您也不必创建4个jquery实例来执行此操作。使用方法链接。@Mereep-也不必创建4个jQuery实例就可以做到这一点。使用方法链接。