如何将 Jquery 响应(带有脚本标签的 HTML 页面)加载到 DOM 中 [英] how to load the Jquery response(HTML page with script tags) into the DOM

查看:25
本文介绍了如何将 Jquery 响应(带有脚本标签的 HTML 页面)加载到 DOM 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

test1.html 文件:

<头><script src="jquery-1.7.2.js"><脚本>函数 loadfn(){$.get('test.html',function(data){警报($(数据).文本());//$('body div#returnData').html(data);$.each(data, function(i, node) {警报(一);if (node.tagName == "SCRIPT") {评估(节点.innerHTML);} 别的 {$(node).appendTo($('#returnData'));}});});测试1();测试2();}<身体><div id="returnData" >

<input type="button" value="Click" onclick=loadfn()>

test.html :

<头><script src="/jquery-ui-timepicker-addon.js"><脚本>功能测试1(){alert('test1 函数');}<脚本>功能测试2(){alert('test2 函数');}<身体><div id="testdiv">文本字段:<input type="text" value="Test Value"/>

实际上,我要求我必须将 test.html 页面加载到 test1.html 中,然后运行该页面中的脚本函数.

以上代码片段不足以满足要求.请让我知道我在代码片段中做错了什么.

解决方案

您不需要将它加载"到您的 DOM 中.对于脚本标签,你可以这样做..

eval("");

更新:

如果结果包含 HTML 和 SCRIPT 节点,请执行以下操作..

$.each(nodes, function(i, node) {//结果可能包含多个 HTML/Script 节点if (node.tagName == "SCRIPT") {评估(节点.innerHTML);//附加到 DOM 也会执行脚本.所以我们用 eval() 代替,它做等价的.} 别的 {$(node).appendTo($('#returnData'));//并将您的 HTML 节点附加到所需的目标 div}});

test1.html file:

<html>
<head>
<script src="jquery-1.7.2.js">
</script>
<script>

function loadfn(){

$.get('test.html',function(data){

alert($(data).text());
//$('body div#returnData').html(data);
$.each(data, function(i, node) { 
    alert(i);
   if (node.tagName == "SCRIPT") {
       eval(node.innerHTML); 

   } else {
       $(node).appendTo($('#returnData')); 
   }

});

});
test1();
test2();
}
</script>
</head>
<body>
<div id="returnData" >
</div>
<input type="button" value="Click" onclick=loadfn()>
</body>
</html>

test.html :

<html>
<head>
<script src="/jquery-ui-timepicker-addon.js">
</script>
</head>
<script>
function test1(){
alert('test1 function');
}
</script>
<script>
function test2(){
alert('test2 function');
}
</script>
<body>
<div id="testdiv">
Text field : <input type="text" value="Test Value"/>
</div>
</body>
</html>

Actually i have the requirement that i have to load the test.html page into test1.html and then run the script functions that are there in that page.

The Above code snippet doesnt suffice the requirement. Please let me know what i am doing wrong in the code snippet.

解决方案

You need not "load" it into your DOM. For script tags, you could just do..

eval("<script>alert('Hi!');</script>");

UPDATE :

If you result contains both HTML and SCRIPT nodes, do the following..

$.each(nodes, function(i, node) { //result might contain muliple HTML/Script nodes

   if (node.tagName == "SCRIPT") {
       eval(node.innerHTML); 
       //Appending to DOM also executes the script. So we do eval() instead, which does the equivalent.
   } else {
       $(node).appendTo($('#returnData')); //And append your HTML nodes to the desired target div
   }

});

这篇关于如何将 Jquery 响应(带有脚本标签的 HTML 页面)加载到 DOM 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆