使用jQuery附加HTML w / click事件 [英] Appending HTML w/click event using jQuery

查看:79
本文介绍了使用jQuery附加HTML w / click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to append some HTML inside this div, and the HTML has to have a click event on it also.

我想在div内追加一些HTML,HTML也必须有一个点击事件。 < div id =myID>
< p> hello,world!< / p>
< / div>

<div id="myID"> <p>hello, world!</p> </div>

我想在上面的div中插入一个新的div,并且我希望在我在那里插入新的HTML。在新的div中会有一些动态文本,所以它必须通过动态创建。

I want to insert a new div inside of the above div, and I want a click event on the new HTML I insert there. There will be some dynamic text inside the new div so it has to by dynamically created.

如何做?

推荐答案

那么:

What about :

$("#myID").click(
  function () {
     var someText = "my dynamic text";
     var newDiv = $("<div>").append(someText).click(function () { alert("click!"); });
     $(this).append(newDiv);
  }
)

这篇关于使用jQuery附加HTML w / click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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