$(文件) . on (“点击”... 不工作?

我是不是犯了一个众所周知的错误?

我有个剧本。On ()是因为一个元素是动态生成的,而且它不工作。为了测试一下,我用动态元素的外壳替换了选择器,它是静态的,但是仍然不能工作!当我变成普通的老人。点击它的包装工作,虽然。
(这显然不适用于动态元素,而动态元素才是最重要的。)

这种方法是有效的:

$("#test-element").click(function() {
alert("click");
});

这不是:

$(document).on("click","#test-element",function() {
alert("click");
});

更新:

我在 Chrome 中右键单击并执行“检查元素”,只是再次检查某些内容,然后单击事件就起作用了。我刷新了一下,它不工作了,检查了元素,然后它工作了。这是什么意思?

484413 次浏览

Try this:

$("#test-element").on("click" ,function() {
alert("click");
});

The document way of doing it is weird too. That would make sense to me if used for a class selector, but in the case of an id you probably just have useless DOM traversing there. In the case of the id selector, you get that element instantly.

This works:

<div id="start-element">Click Me</div>


$(document).on("click","#test-element",function() {
alert("click");
});


$(document).on("click","#start-element",function() {
$(this).attr("id", "test-element");
});

Here is the Fiddle

You are using the correct syntax for binding to the document to listen for a click event for an element with id="test-element".

It's probably not working due to one of:

  • Not using recent version of jQuery
  • Not wrapping your code inside of DOM ready
  • or you are doing something which causes the event not to bubble up to the listener on the document.

To capture events on elements which are created AFTER declaring your event listeners - you should bind to a parent element, or element higher in the hierarchy.

For example:

$(document).ready(function() {
// This WILL work because we are listening on the 'document',
// for a click on an element with an ID of #test-element
$(document).on("click","#test-element",function() {
alert("click bound to document listening for #test-element");
});


// This will NOT work because there is no '#test-element' ... yet
$("#test-element").on("click",function() {
alert("click bound directly to #test-element");
});


// Create the dynamic element '#test-element'
$('body').append('<div id="test-element">Click mee</div>');
});

In this example, only the "bound to document" alert will fire.

JSFiddle with jQuery 1.9.1

Your code should work, but I'm aware that answer doesn't help you. You can see a working example here (jsfiddle).

Jquery:

$(document).on('click','#test-element',function(){
alert("You clicked the element with and ID of 'test-element'");
});

As someone already pointed out, you are using an ID instead of a class. If you have more that one element on the page with an ID, then jquery will return only the first element with that ID. There won't be any errors because that's how it works. If this is the problem, then you'll notice that the click event works for the first test-element but not for any that follow.

If this does not accurately describe the symptoms of the problem, then perhaps your selector is wrong. Your update leads me to believe this is the case because of inspecting an element then clicking the page again and triggering the click. What could be causing this is if you put the event listener on the actual document instead of test-element. If so, when you click off the document and back on (like from the developer window back to the document) the event will trigger. If this is the case, you'll also notice the click event is triggered if you click between two different tabs (because they are two different documents and therefore you are clicking the document.

If neither of these are the answer, posting HTML will go a long way toward figuring it out.

An old post, but I love to share as I have the same case but I finally knew the problem :

Problem is : We make a function to work with specified an HTML element, but the HTML element related to this function is not yet created (because the element was dynamically generated). To make it works, we should make the function at the same time we create the element. Element first than make function related to it.

Simply word, a function will only works to the element that created before it (him). Any elements that created dynamically means after him.

But please inspect this sample that did not heed the above case :

<div class="btn-list" id="selected-country"></div>

Dynamically appended :

<button class="btn-map" data-country="'+country+'">'+ country+' </button>

This function is working good by clicking the button :

$(document).ready(function() {
$('#selected-country').on('click','.btn-map', function(){
var datacountry = $(this).data('country'); console.log(datacountry);
});
})

or you can use body like :

$('body').on('click','.btn-map', function(){
var datacountry = $(this).data('country'); console.log(datacountry);
});

compare to this that not working :

$(document).ready(function() {
$('.btn-map').on("click", function() {
var datacountry = $(this).data('country'); alert(datacountry);
});
});

hope it will help

if this code does not work even under document ready, most probable you assigned a return false; somewhere in your js file to that button, if it is button try to change it to a ,span, anchor or div and test if it is working.

$(document).on("click","#test-element",function() {
alert("click bound to document listening for #test-element");
});