简体   繁体   English

尝试为div创建click方法,在IE6中不起作用吗?

[英]Trying to create a click method for a div, doesn't work in IE6?

I made a simple div: 我做了一个简单的div:

<div id="foo"></div>

and I added a click method to it in jquery 我在jQuery中添加了一个点击方法

$("#foo").click(wow);

function wow(){
  alert("Message");
}

This works fine in Firefox/others, but doesn't work in IE6/IE7? 在Firefox /其他浏览器中可以正常工作,但在IE6 / IE7中不能正常工作吗? Why? 为什么? Any specific resources on this? 有什么具体的资源吗?

Edit: The Div already exists in the dom, and the jQuery is run on ready, and it still doesn't work. 编辑:div已经存在于dom中,并且jQuery已准备就绪运行,但仍无法正常工作。

If you're creating the div dynamically, it won't work, and it won't work if that code is run before $(document).ready() . 如果要动态创建div,它将无法正常工作,并且如果该代码在$(document).ready()之前运行,它将无法正常工作。

The solutions to both problems, respective, are to use jQuery's live mechanism if you're creating dynamic divs and to place all jQuery event binding in the document ready of the document. 如果要创建动态div,则分别解决这两个问题的方法是使用jQuery的live机制,并将所有jQuery事件绑定都放置在准备好文档的文档中。

Also make sure that your click handler function is defined before you assign it to the .click method. 另外,在将您的点击处理函数分配给.click方法之前 ,请确保已定义该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM