2012-07-25 46 views
1

我遇到了运行在JQuery下运行的第二个进程的问题,如下面的代码。如何重新加载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加载有关吗?

感谢

回答

1

您可以使用.live()的ID的DOM元素点击事件绑定( )在运行时绑定事件。由于.live()已被弃用,您可能需要尝试使用.on()方法。

+0

工作!谢谢 – 2012-07-25 07:45:37

1

你正在试图绑定一个处理程序,不存在的元素,试试这个:

<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和他的处理程序。

+0

现在我得到了一个更清晰的图片。谢谢你的伟大答案! – 2012-07-25 06:20:43

1

将点击事件添加到ip2时,它不存在。您可以使用jquery'live'将事件绑定到页面上稍后显示的任何内容上。

$('#ip2').live('click',function() {}); 

这将在被添加到文档以后,如果他们有代替。点击“IP2”

+0

工作!谢谢 – 2012-07-25 07:40:50

相关问题