绑定由jQuery .html()函数插入的元素的事件处理函数 [英] Bind an event handler on a element who's inserted by the jQuery .html() function

查看:107
本文介绍了绑定由jQuery .html()函数插入的元素的事件处理函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ajax调用我的网站后,我使用.html()呈现了一些新内容。

I render some new content with .html() after ajax call into my site.

$.getJSON(scriptURL, $("#domainForm").serialize(), function(data) {
  $("#checkedDomain").html(data['html']) 
});

现在。我怎样才能绑定一个事件处理程序div标签在替换的HTML不包括脚本再次?
现在我呈现在我的网站中的html看起来像这样:

Now. How can I bind an event handler to div tags in that replaced html without including the script again? now the html the I render into my site looks like this:

<script type="text/javascript" src="myScript.js"/>
<div id="tagWithHandlerOn"/>someButton</div>

我想摆脱< script>标签,因为它是e重新声明,如果我再次加载相同的内容到该标签2脚本将被加载。任何提示?

I want to get rid of the <script> Tag because it's e redeclaration and if I load the same content into that tag again 2 scripts gonna be loaded. Any hint?

推荐答案

尝试使用 jQuery.live()


jQuery 1.7的,不赞成使用 .live()方法。使用 .on()附加事件处理程序。老版本jQuery的用户应优先使用 .delegate()。.live() p>

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

这篇关于绑定由jQuery .html()函数插入的元素的事件处理函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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