Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将脚本运行到从jquery加载特定标记加载的另一个页面_Javascript_Jquery - Fatal编程技术网

Javascript 如何将脚本运行到从jquery加载特定标记加载的另一个页面

Javascript 如何将脚本运行到从jquery加载特定标记加载的另一个页面,javascript,jquery,Javascript,Jquery,装载机 $( "#b" ).load( "thepage.html #target" ); <div id="b"></div> $(“#b”).load(“thepage.html#target”); thepage.html <div id="target"> <script> alert('the page'); </script> hi world </div> 警报(“页面”); 嗨,世界 当从jque

装载机

$( "#b" ).load( "thepage.html #target" );

<div id="b"></div>
$(“#b”).load(“thepage.html#target”);
thepage.html

<div id="target">
<script>
alert('the page');
</script>
hi world
</div>

警报(“页面”);
嗨,世界
当从jquery函数加载“thepage.html”时,如果您有以下情况,我需要显示警报:

$( "#b" ).load( "thepage.html #target" );

<div id="b"></div>

编辑

从JQuery API:

如果调用
.load()
,并将选择器表达式附加到URL, 但是,在更新DOM之前,脚本将被剥离, 因此没有执行。两种情况的示例如下所示:

在这里,作为文档的一部分加载到#a中的任何JavaScript都将 成功执行

$( "#a" ).load( "article.html" );
但是,在下面的情况下,要删除的文档中的脚本块 加载到#b中的数据被剥离,且未执行:

$( "#b" ).load( "article.html #target" );
加载完成后,您必须执行JS。由于安全问题,您将无法从页面加载脚本

你可以试试这个:

<div id="target">
  Hello World.
  <div style="display: none;" id="alertMessage">The Page</div>
</div>

您的脚本未包含在target
#target
部分中,因此它为什么要加载?准备好了,但同样无法工作编辑我的答案。我希望这有助于解决您的问题。
<div id="target">
  Hello World.
  <div style="display: none;" id="alertMessage">The Page</div>
</div>
$("#b").load("thepage.html #target", function(r, s, x){
    if(s != "error"){
       alert($("#alertMessage").text());
    }
});