开发者

Javascript: add onclick attribute DOM

开发者 https://www.devze.com 2023-02-13 14:02 出处:网络
I\'ve tryed to add some listeners to an existing div element //Prepare div $(this.div).css(\"z-index\",\"256\");

I've tryed to add some listeners to an existing div element

        //Prepare div
        $(this.div).css("z-index","256");
        $(this.div).attr("onmouseover",this.myname+".resize();");
        $(this.div).attr("onmousedown",this.myname+".resize();");
  开发者_如何学Python      $(this.div).attr("onmouseout","if("+this.myname+".sized)"+ this.myname+".resize();");

but in IE and Chrome the Event just doesn't get fired while it still gets added to the elements attributes. Firefox works as expected.

Does someone know whats wrong with it?

Thanks


Do not set events as strings.
Instead, you should use jQuery's bind method:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消