Click事件在jQuery中不起作用 [英] Click event not working in jQuery

查看:106
本文介绍了Click事件在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆