首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何重新加载jquery以使其在下一个进程中工作

如何重新加载jquery以使其在下一个进程中工作
EN

Stack Overflow用户
提问于 2012-07-25 13:58:59
回答 3查看 82关注 0票数 1

我在运行第二个在JQuery下运行的进程时遇到了问题,如下所示。

代码语言:javascript
复制
<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加载有关?

谢谢

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-07-25 14:07:10

您可以使用.live()而不是.click()在运行时绑定事件。由于.live()已被弃用,因此您可能需要尝试.on()方法。

票数 1
EN

Stack Overflow用户

发布于 2012-07-25 14:04:34

您正在尝试将处理程序绑定到一个不存在的元素,请尝试执行以下操作:

代码语言:javascript
复制
<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和他的处理程序。

票数 1
EN

Stack Overflow用户

发布于 2012-07-25 14:06:59

当您将单击事件添加到ip2上时,它并不存在。您可以使用jquery 'live‘将事件绑定到稍后出现在页面上的任何内容。

代码语言:javascript
复制
$('#ip2').live('click',function() {});

这将绑定稍后添加到文档中的任何dom元素上的click事件(如果它们的id为'ip2‘)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11643564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档