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

查看:33
本文介绍了在 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']) 
});

现在.如何将事件处理程序绑定到替换后的 html 中的 div 标签而不再次包含脚本?现在我呈现到我的网站的 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>标记,因为它是重新声明,如果我再次将相同的内容加载到该标记中,将加载 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().>

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天全站免登陆