使用内联javascript加载jquery

使用内联javascript加载jquery,javascript,jquery,Javascript,Jquery,我使用jQueryLoad在不同的页面上获取一个div,并将其插入到我的页面中。 像这样的事情: $('#mydiv').load("/Pages/grid2.aspx" + " #otherpagediv"); 在另一页的div中,div中有javascript。javascript没有出现,只有html内容。是否有方法获取指定div中的所有内容?JavaScript也应随响应一起提供。您必须确保/Pages/grid2.aspx应该从服务器端发送所需的响应。另外,传递给load方法的url

我使用jQueryLoad在不同的页面上获取一个div,并将其插入到我的页面中。 像这样的事情:

$('#mydiv').load("/Pages/grid2.aspx" + " #otherpagediv");

在另一页的div中,div中有javascript。javascript没有出现,只有html内容。是否有方法获取指定div中的所有内容?

JavaScript
也应随响应一起提供。您必须确保
/Pages/grid2.aspx
应该从服务器端发送所需的响应。另外,传递给
load
方法的url中有一个空格。我认为你应该纠正这一点并尝试一下

$('#mydiv').load("/Pages/grid2.aspx" + "#otherpagediv");
发件人:

注意:使用不带后缀选择器的URL调用.load()时 表达式,则在创建脚本之前将内容传递到.html() 远离的。这将在脚本块被丢弃之前执行它们

但是,如果调用.load(),则将选择器表达式附加到 URL,则在更新DOM之前剥离脚本, 这就是为什么他们从未被处决

这项工作:

$.get( '/Pages/grid2.aspx', function ( data ) {
    $( '#mydiv' ).html( $( '<div></div>' ).html( data ).find( '#otherpagediv' ).clone() );
});
$.get('/Pages/grid2.aspx',函数(数据){
$('.''.'mydiv').html($('..html(数据).find('.'其他页面div').clone());
});
现场演示:

要理解演示,请查看组成演示的两个页面的源代码:
演示的源代码:
“其他页面”的源代码:

想法是通过
$检索另一个页面。获取
请求,然后找到
#otherpagediv
克隆它。然后将克隆附加到
#mydiv
。如果将克隆插入DOM,并且该克隆包含脚本元素,则将执行该脚本

您可能会发现此页面很有用,我使用了脚本,它使用eval()

//此函数创建一个数组,其中包含每个
//然后应用eval()执行收集的每个脚本中的代码
函数解析脚本(strcode){
var scripts=new Array();//将存储脚本代码的数组
//去掉标签

while(strcode.indexOf)(“您能为我们提供加载的DIV的Javascript吗?代码是初始的?这实际上是通过设计
load
函数实现的。该空间用于加载页面的一部分。这应该明确地添加到jQuery API页面中!
// this function create an Array that contains the JS code of every <script> 
// then apply the eval() to execute the code in every script collected
 function parseScript(strcode) {
 var scripts = new Array();         // Array which will store the script's code

// Strip out tags
 while(strcode.indexOf("<script") > -1 || strcode.indexOf("</script") > -1) {
 var s = strcode.indexOf("<script");
 var s_e = strcode.indexOf(">", s);
 var e = strcode.indexOf("</script", s);
 var e_e = strcode.indexOf(">", e);

 // Add to scripts array
 scripts.push(strcode.substring(s_e+1, e));
 // Strip from strcode
 strcode = strcode.substring(0, s) + strcode.substring(e_e+1);
}

// Loop through every script collected and eval it
for(var i=0; i<scripts.length; i++) {
 try {
   eval(scripts[i]);
 }
  catch(ex) {
    // do what you want here when a script fails
  }
 }
}