简体   繁体   English

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

[英]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.

相关问题 页面加载后如何可靠地运行jquery脚本 - How can I reliably run a jquery script after page load 如何从另一个使用jQuery load()加载的html页面执行写在主html文件中的脚本 - How to execute script written in main html file from another html page loaded using jquery load() 如何在从另一个页面加载的模式中运行脚本? - How to run a script inside a modal that is loaded from another page? 如何在每个页面加载后运行特定的 jquery 脚本? - How to run a specific jquery script after each page load? 加载 HTML 页面时如何运行 Python 脚本? - How can I run a Python script when an HTML page is loaded? 在页面加载时运行的jquery脚本之后,如何运行C#代码? - How can I have C# code run after a jquery script that is running on page load? 如何从其中的另一个jquery脚本运行.load() - How to run a .load() from a another jquery script inside it 页面加载后如何加载脚本标签? - How to load script tag after page has loaded? 当我单击锚标记时,页面重定向到另一个页面。 然后我需要使用 Java 脚本加载特定内容 - When I click the anchor tag, page re-direct to to another page. Then I need to load specific content using Java Script 如果尚未加载 jQuery,如何加载它? - How can I load jQuery if it is not already loaded?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM