[英]how i can run a script to another page loaded from jquery load specific tag
The loader 装载机
$( "#b" ).load( "thepage.html #target" );
<div id="b"></div>
thepage.html thepage.html
<div id="target">
<script>
alert('the page');
</script>
hi world
</div>
I need when 'thepage.html' is loaded from the jquery function display the alert 我需要从jQuery函数加载“ thepage.html”时显示警报
If you have this: 如果您有这个:
$( "#b" ).load( "thepage.html #target" );
<div id="b"></div>
And you want the JS to execute, you need to have a
#target
like so:
而且,您想让JS执行,就需要像这样的
#target
:
<div id="target"> <script> alert('the page'); </script> hi world </div>
EDIT 编辑
From the JQuery API: 从JQuery API:
If
.load()
is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated , and thus are not executed. 但是,如果使用附加到URL的选择器表达式调用.load()
, 则脚本将在DOM更新之前被删除 ,因此不会执行。 An example of both cases can be seen below: 两种情况的示例如下所示:Here, any JavaScript loaded into #a as a part of the document will successfully execute. 在这里,任何作为文档一部分加载到#a中的JavaScript都将成功执行。
$( "#a" ).load( "article.html" );
However, in the following case, script blocks in the document being loaded into #b are stripped out and not executed: 但是,在以下情况下,将文档中正在加载到#b中的脚本块删除并且不执行:
$( "#b" ).load( "article.html #target" );
You will have to execute the JS once the Load completes. 加载完成后,您将必须执行JS。 You won't be able to load the script from the page due to security concerns. 出于安全考虑,您将无法从页面加载脚本。
Could try this: 可以尝试一下:
<div id="target">
Hello World.
<div style="display: none;" id="alertMessage">The Page</div>
</div>
Then execute like so: 然后像这样执行:
$("#b").load("thepage.html #target", function(r, s, x){
if(s != "error"){
alert($("#alertMessage").text());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.