Jquery .load() - 如何从加载的内容中执行原始文档中的 Javascript

发布于 2024-12-27 17:23:55 字数 1292 浏览 0 评论 0原文

我有一个文档,它使用 JQuery 的 .load() 函数将一些内容加载到 div 中。然后,加载的内容尝试从原始文档执行 Javascript。这失败了,可能有明显的原因(我是一个 Javascript 新手)。这是说明我的问题的简化代码。

test.html:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <style type="text/css">
    div {
        background: #ccc;
        margin: 15px;
        height: 500px;
        width: 500px;
        border: 1px solid #666;
    }
    </style>
</head>
<body>
    <a href="#" class="loader">Load content into box below</a>
    <div id="loadbox"></div>
    <script type="text/javascript"> 

        jQuery(document).ready(function() { 

            $('a.loader').click(function() {
                $('#loadbox').load('loadme.html');
            });

            $('a.alerter').click(function() {
                alert("I've been clicked!");
            });

        });
    </script>
</body>
</html>

loadme.html:

<a href="#" class="alerter">Click me</a> for an alert.

如果我将 Javascript 放入加载的内容中,则会触发警报,但我有一大块 Javascript,我想将其与多个充满加载内容的 div 一起使用,但我不希望继续加载 Javascript。有没有办法从加载的内容访问原始 HTML 中的 Javascript?我不太清楚事情的执行顺序,特别是当我将内容加载到页面中时。也许这就是我学习的机会吧!谢谢。

I have a document that loads some content into a div with JQuery's .load() function. That loaded content then tries to execute Javascript from the original document. This fails, possibly for obvious reasons (I am a Javascript novice). Here is simplified code that illustrates my problem.

test.html:

<html>
<head>
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <style type="text/css">
    div {
        background: #ccc;
        margin: 15px;
        height: 500px;
        width: 500px;
        border: 1px solid #666;
    }
    </style>
</head>
<body>
    <a href="#" class="loader">Load content into box below</a>
    <div id="loadbox"></div>
    <script type="text/javascript"> 

        jQuery(document).ready(function() { 

            $('a.loader').click(function() {
                $('#loadbox').load('loadme.html');
            });

            $('a.alerter').click(function() {
                alert("I've been clicked!");
            });

        });
    </script>
</body>
</html>

loadme.html:

<a href="#" class="alerter">Click me</a> for an alert.

The alert is triggered if I put the Javascript in the loaded content, but I have a large block of Javascript that I want to use with more than one div full of loaded content and I don't want to keep loading the Javascript. Is there a way to access the Javascript in the original HTML from the loaded content? I don't have a firm understanding of what order things are executed in, especially when I have loaded content into the page. Maybe this is my opportunity to learn! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独留℉清风醉 2025-01-03 17:23:55

D您应该使用 live()delegate() 因为您要向文档添加一个元素(这适用于 jQuery < 1.7),

   jQuery(document).ready(function() { 

        $('a.loader').click(function() {
            $('#loadbox').load('loadme.html');
        });

        $('a.alerter').live("click", function() {
            alert("I've been clicked!");
        });

    });

对于 jQuery > 1.7 您应该使用 on() 并将处理程序委托给主体

   jQuery(document).ready(function() { 

        $('a.loader').click(function() {
            $('#loadbox').load('loadme.html');
        });

        $('body').on("click", "a.alerter", function() {
            alert("I've been clicked!");
        });

    });

DYou should use live() or delegate() because you are adding an element to the document (this is for jQuery < 1.7)

   jQuery(document).ready(function() { 

        $('a.loader').click(function() {
            $('#loadbox').load('loadme.html');
        });

        $('a.alerter').live("click", function() {
            alert("I've been clicked!");
        });

    });

for jQuery > 1.7 you should use on() and delegate the handler to the body

   jQuery(document).ready(function() { 

        $('a.loader').click(function() {
            $('#loadbox').load('loadme.html');
        });

        $('body').on("click", "a.alerter", function() {
            alert("I've been clicked!");
        });

    });
独留℉清风醉 2025-01-03 17:23:55

因为你加载的dom不在这里,脚本不可能找不到它; ,请尝试一下:

jQuery(document).ready(function () {
    $('a.loader').click(function () {
        $('#loadbox').load('loadme.html', function () {
            $('a.alerter').click(function () {
                alert("I've been clicked!");
            });
        });
    });
});

Because the dom u load is not here and the script can't not find it ; , plz try it :

jQuery(document).ready(function () {
    $('a.loader').click(function () {
        $('#loadbox').load('loadme.html', function () {
            $('a.alerter').click(function () {
                alert("I've been clicked!");
            });
        });
    });
});
仙女 2025-01-03 17:23:55

替换

$('a.alerter').click(function() {
    alert("I've been clicked!");
});

$("#loadbox").delegate( "a.alerter", "click", function() {
    alert("I've been clicked!");
});

这会将事件委托给存在的 #loadbox,因此只要 #loadbox 存在,它就会工作。

Replace

$('a.alerter').click(function() {
    alert("I've been clicked!");
});

With

$("#loadbox").delegate( "a.alerter", "click", function() {
    alert("I've been clicked!");
});

This will delegate the event to #loadbox, which exists, so it will work as long as #loadbox exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文