与jQuery的AJAX加载页面preventing脚本执行 [英] Preventing script execution on ajax load pages with jQuery

查看:110
本文介绍了与jQuery的AJAX加载页面preventing脚本执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用load()函数加载一个页面,问题是,JavaScript的加载时正在执行该网页上code。我用这样的:

I'm trying to load a page using the load() function, the problem is that javascript code on that page is being executed when loading. I use this:

$('#itemid').load('thepage.php #selector', function() {
     MY CODE HERE
});

我怎么能prevent从JavaScript的code正在执行和负载只是我想要的HTML部分?

how can i prevent the javascript code from being executed and load only the HTML part that i want?

推荐答案

使用获得()。员额()和工艺你会得到什么。您将追加到页面之前,请先拉出脚本标记出你回来code,或者只是拔出你想要的code:

Use .get() or .post() and process what you get back. Pull the script tags out of your returned code before you append it to the page, or just pull out the code you want:

$.post('thepage.php', {
    data: myData
}, function(data) {
    var myHTML = $(data).not('script');
    $('#itemid').html(myHTML);
});

或者

$.post('thepage.php', {
    data: myData
}, function(data) {
    var myHTML = $(data).filter('#selector');
    $('#itemid').html(myHTML);
});

演示: http://jsfiddle.net/jtbowden/wpNBM/

注意:正如你所说,使用选择与负载要做到同样的事情,因为你看到的例子。所以,如果不是这种方式工作,别的回事。

Note: As you mentioned, using a selector with load should accomplish the same thing, as you see in the example. So, if it isn't working this way, something else is going on.

这篇关于与jQuery的AJAX加载页面preventing脚本执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆