代码之家  ›  专栏  ›  技术社区  ›  Funka

如何获取嵌入在通过jquery load()方法检索的内容中的脚本?

  •  4
  • Funka  · 技术社区  · 16 年前

    我在S.O.(以及整个网络)上发现了几个其他问题,它们提出了大致相同的问题,但答案似乎总是暗示了构建代码的其他方式,从而避免了解决这一根本问题的需要。

    下面是一个测试设置来演示我要做的事情。 简短的总结是当我加载 partial.htm 从…起 main.htm ,它的脚本不会启动。

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>main file</title>
    </head>
    <body>
    <ul id="links">
        <li><a href="somepage1.php">some page1</a></li>
        <li><a href="somepage2.aspx">some page 2</a></li>
        <li><a href="partial.htm">some other partial page</a></li>
    </ul>
    <div id="panel" style="display:none; padding:20px; background-color:#CCC;">
    LOADED CONTENT WILL GO HERE
    </div>
    <script type="text/javascript" src="/path/to/jquery-1.3.2.min.js"> </script>
    <script type="text/javascript">
    $(function(){
        $('#links a').click(function() {
            var $panel = $('#panel');
            $panel.show();
            $panel.html('Please wait...');
            var href = $(this).attr('href');
            $('#panel').load(href + ' #content');
            return false;
        });
    });
    </script>
    </body>
    </html>
    

    好的,这个页面上的功能非常简单。假设还有更多的链接,其中一些可能需要脚本,而另一些则不需要。

    这是 partial.htm :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>partial file</title>
    </head>
    <body>
    <div id="content">
        <p>Hey, I am the partial file!</p>
        <script type="text/javascript">
        alert('I am some JS in the partial file! But sadly I do not execute...');
        </script>
    </div>
    <div>
        I am some other content on the page that won't be included by jquery.load()...
    </div>
    </body>
    </html>
    

    排除

    非常感谢。


    更新#1:

    另外,当我使用firebug检查 panel 区域之后,根本不存在脚本元素。这就好像它是由 load .


    只有 将选择器用作href的一部分时。加载整个 "somepage.html" 将执行脚本,但正在加载 "somepage.html #someregion" 没有。

    $panel.load('somepage.html');              // my script fires!
    $panel.load('somepage.html #someregion');  // script does not fire
    

    2 回复  |  直到 16 年前
        1
  •  4
  •   Funka    16 年前

    看来这是故意的。显然是为了让IE开心,我们其他人都在受苦。以下是jquery源代码中的相关代码:

    // See if a selector was specified
    self.html( selector ?
        // Create a dummy div to hold the results
        jQuery("<div/>")
    
            // inject the contents of the document in, removing the scripts
            // to avoid any 'Permission Denied' errors in IE
            .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, ""))
    
            // Locate the specified elements
            .find(selector) :
    
        // If not, just inject the full result
        res.responseText );
    

    我想知道,除了剥离脚本,我是否可以修改jquery源代码,以使IE满意的其他方式包含它们?我还没有在网上找到任何其他关于这件事的讨论,我肯定我不是唯一一个被这件事难倒的人?

        2
  •  0
  •   Kevin Hakanson    16 年前

    <script> 不包含延迟属性的。此讨论线程提供了有关此主题的一些好信息: innerHTML and SCRIPT tag