2012-07-25 47 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

相關問題