jQuery按钮不响应点击方法 [英] jquery button not respond to click method

查看:155
本文介绍了jQuery按钮不响应点击方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我动态渲染一个段落与jquery使用append方法,我想添加一个点击事件,但由于某种原因点击事件不工作,我知道解决方案可能很简单,但我是新的jquery并且会喜欢任何帮助...我知道函数内部的代码工作,因为我测试它与一个静态按钮,它只是不能与动态一起工作。提前为任何帮助,

so i am dynamically rendering a paragraph with jquery using the append method and i want to add a click event to it but for some reason the click event is not working, i know the solution is probably simple but I am new to jquery and would appreciate any help...I know the code inside the function works because i tested it with a static button, it is just not working with the dynamic one..Thanks in advance for any help,

这里是我的代码

$(this).parent().parent().children("div").append("<p class='tryAgain'>Try Again</p>");

点击功能代码,

$(".tryAgain").click(function() {......}


推荐答案

在document.ready触发后添加到DOM的任何内容都需要使用。live .delegate ,以便添加事件处理程序到新添加的元素。

Anything you add to the DOM after the document.ready has fired needs to use .live or .delegate in order to add an event handler to the newly added element.

例如:

$('.tryAgain').live("click", function() {...});

如果您使用的是jquery 1.7+,则应使用 .on

If you are using jquery 1.7+ you should use .on:

$(document).on("click", ".tryAgain", function(){ ... });

这篇关于jQuery按钮不响应点击方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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