我在运行第二个在JQuery下运行的进程时遇到了问题,如下所示。
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /></head>
<body>
<script type="text/javascript" src='jquery-1.6.2.min.js'> </script>
<div id="displayArea1">
</div>
<br/>
<div id="displayArea2">
</div>
<input id="btnSet" type="button" value="Set The Value" />
<script>
//The input that should i use
var itemToPrintOut1 = "[Input1 (working)] Search <strong style='cursor:pointer' title='Click here'>IP{<span id='ip1'>192.168.2.56,192.168.2.89,192.168.2.75</span>}</strong> that has accessed to MAIL-SERVER{192.168.2.8}";
var itemToPrintOut2 = "[Input2 (not working)] Search <strong style='cursor:pointer' title='Click here'>IP{<span id='ip2'>192.168.2.56,192.168.2.89,192.168.2.75</span>}</strong> that has accessed to MAIL-SERVER{192.168.2.8}";
$("#displayArea1").html(itemToPrintOut1); //If I enable this line, it is works as what i want(because it is using jquery)
$("#btnSet").click(function(){
$("#displayArea2").html(itemToPrintOut2);
});
$("#ip1").click(function(){
alert("Normal method, data = " + $("#ip1").html());
});
$("#ip2").click(function(){
alert("The method I should use, data = " + $("#ip2").html());
});
</script>
</body>
第一次(加载时)它将显示itemToPrintOut1文本,我可以单击IP地址。我对此没有意见!
但当我单击该按钮并显示第二个文本itemToPrintOut2时,我无法再单击该IP。
有没有人可以帮助我确保第二个输出也可以和JQUERY一起使用。对我来说,与这个问题相关的事情太多了。是否与JQuery加载有关?
谢谢
发布于 2012-07-25 14:07:10
您可以使用.live()而不是.click()在运行时绑定事件。由于.live()已被弃用,因此您可能需要尝试.on()方法。
发布于 2012-07-25 14:04:34
您正在尝试将处理程序绑定到一个不存在的元素,请尝试执行以下操作:
<script>
//The input that should i use
var itemToPrintOut1 = "[Input1 (working)] Search <strong style='cursor:pointer' title='Click here'>IP{<span id='ip1'>192.168.2.56,192.168.2.89,192.168.2.75</span>}</strong> that has accessed to MAIL-SERVER{192.168.2.8}";
var itemToPrintOut2 = "[Input2 (not working)] Search <strong style='cursor:pointer' title='Click here'>IP{<span id='ip2'>192.168.2.56,192.168.2.89,192.168.2.75</span>}</strong> that has accessed to MAIL-SERVER{192.168.2.8}";
$("#displayArea1").html(itemToPrintOut1); //If I enable this line, it is works as what i want(because it is using jquery)
$("#btnSet").click(function(){
$("#displayArea2").html(itemToPrintOut2);
$("#ip2").click(function(){
alert("The method I should use, data = " + $("#ip2").html());
});
});
$("#ip1").click(function(){
alert("Normal method, data = " + $("#ip1").html());
});
</script>然后,当您单击ip1时,它会添加ip2 html和他的处理程序。
发布于 2012-07-25 14:06:59
当您将单击事件添加到ip2上时,它并不存在。您可以使用jquery 'live‘将事件绑定到稍后出现在页面上的任何内容。
$('#ip2').live('click',function() {});这将绑定稍后添加到文档中的任何dom元素上的click事件(如果它们的id为'ip2‘)
https://stackoverflow.com/questions/11643564
复制相似问题