Click事件在jQuery中不起作用 [英] Click event not working in jQuery
问题描述
我有html,我需要在表中追加一行动态,也需要捕获append div类中的click事件。如何实现这个想法?
JavaScript:
$(document).ready(function(){
$ (#click)。click(function(){
$('table').prepend('< tr>< td> something3< / td>< td>< a>< div class =s> s< / a>< / td>< / tr>');
});
$(。 ()函数(){
alert(Ok);
});
});
HTML:
< input type =buttonid =clickvalue =click/>
< table width ='100%'>
< tr>< td>东西< / td>< td>其他< / td>< / tr>
< tr>< td> something2< / td>< td> else here2< / td>< / tr>
< tr>< td> something3< / td>< td> else here3< / td>< / tr>
< / table>
请参阅 live()
方法,它允许您为所有当前元素和将来元素绑定一个事件在DOM中,匹配给定的选择器。
$ $ $ $ $s。live('click',function() {
alert(Ok);
});
jQuery文档说:
为所有匹配当前选择器的元素添加一个处理程序未来
注意 2011年10月,当jQuery 1.6.4成为国王时。 live()
在1.7中被弃用,而使用 上()
。现在是等价的语法;
pre $ $($)$(document).on(click,.s,function(){
alert(Ok);
});
I have html , I need to append a row in a table dynamically, also i need to catch the click event in the append div class.How to achieve this any idea?
JavaScript:
$(document).ready(function() {
$("#click").click(function() {
$('table').prepend('<tr> <td>something3</td> <td> <a><div class="s"> s </a> </td></tr>');
});
$(".s").click(function() {
alert("Ok");
});
});
HTML:
<input type="button" id="click" value="click"/>
<table width='100%'>
<tr><td>something</td><td>else here</td></tr>
<tr><td>something2</td><td>else here2</td></tr>
<tr><td>something3</td><td>else here3</td></tr>
</table>
See the live()
method, it allows you to bind an event for all current, and future elements in the DOM that match the given selector.
$(".s").live('click', function(){
alert("Ok");
});
jQuery docs say:
Attach a handler to the event for all elements which match the current selector, now and in the future
Note that this answer was written in October 2011, when jQuery 1.6.4 was king. live()
was deprecated in 1.7 in favour of using on()
. The equivalent syntax is now;
$(document).on("click", ".s", function(){
alert("Ok");
});
这篇关于Click事件在jQuery中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!