admin管理员组文章数量:1321435
When using
$('.foo').click(function(){
alert("I haz class alertz!");
return false;
});
in application.js, and
<a href = "" class = "foo" id = "foobar_1" >Teh Foobar </a>
in any div that initializes with the page, when clicking "Teh Foobar" it alerts and doesn't follow the link. However, when using the same code in application.js, and
<a href = "" class = "foo" id = "foobar_1" >Teh Foobar </a>
is being returned into a div by a
form_remote_tag
when clicked, "Teh Foobar" fails to alert, and functions as a link.
What is happening, and how do I get around it?
When using
$('.foo').click(function(){
alert("I haz class alertz!");
return false;
});
in application.js, and
<a href = "" class = "foo" id = "foobar_1" >Teh Foobar </a>
in any div that initializes with the page, when clicking "Teh Foobar" it alerts and doesn't follow the link. However, when using the same code in application.js, and
<a href = "" class = "foo" id = "foobar_1" >Teh Foobar </a>
is being returned into a div by a
form_remote_tag
when clicked, "Teh Foobar" fails to alert, and functions as a link.
What is happening, and how do I get around it?
Share Improve this question edited Oct 15, 2008 at 2:09 DA. asked Oct 7, 2008 at 23:55 DA.DA. 9401 gold badge7 silver badges10 bronze badges3 Answers
Reset to default 3New elements added to the document after you bind your events don't automatically get those event handlers. One way to fix this is - as John Millikin says - re-bind your events after you create new elements.
The other standard way is event delegation. Because events go all the way up and down the stack through all their parent elements, you can bind an event to an element that will be an ancestor of all your target elements.
For instance, this jQuery code would work (your syntax may vary for other JavaScript libraries):
$(document).ready(function() {
$('body').click(function(event) {
if ($(event.target).is('.foo')) { // <- this is the magic
alert('Something of class foo was clicked.');
return false;
}
});
});
Now when you click something of class foo this event will get fired unless something in between catches the event and cancels the bubbling. Actually, event will be called when almost anything is clicked - the "if" statement just filters out which events deserve the alert.
Markup returned from an AJAX call isn't present when you set up the page, so it doesn't have any onclick handlers associated with it. You'll need to hook into the Rails AJAX support so that when it loads your AJAX-powered div, it also executes your event setup code again.
You could also use Live Query jQuery plugin which is able to automatically bind events for matched elements after the DOM is updated. In your case it would be:
$('.foo').livequery('click', function() {
alert("I haz class alertz!");
return false;
});
本文标签:
版权声明:本文标题:javascript - JQuery selectors not finding class on elements in table created by an Ajax XHR in Ruby on Rails - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742102298a2420856.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论