内联jQuery脚本AJAX调用中不工作 [英] Inline jQuery script not working within AJAX call
问题描述
我有一个问题:
而我所谓的内嵌脚本(至极使用jQuery的太)从另一个页面使用Ajax - (?)看来,这jQuery是没有更多的定义,我不能使用任何的jQuery的职能,应适用(根据内嵌脚本)内容。
I have a issue:
while i call a inline script (wich uses jQuery too) from another page with ajax - it seems, that jQuery is no more defined (?), and I cannot use any of jQuery functions, that should be applied (according to inline script) to content.
这基本上是一个新闻列表,其中包含链接到特定的新闻项目。我使用内联脚本在这个时候,因为我不需要在其他地方这个功能preFER。
It's basically a news list, which holds links to particular news items. I prefer using inline-script at this time, because I won't need this functionality elsewhere.
$.ajax({
url: href,
cache: false,
success: function(html){
$('#fancy_ajax').append($(html).find('.mainContentPadded'));
}
});
正如你可以看到,我只是调用另一个页面的一部分,并追加其内容的页面。
As you can see, I'm simply calling a part of another page and appending its contents to page.
当我加载整个页面(而不是它的一部分) - jQuery的工作正常(这就是为什么我遇到的想法,它需要rebinded)
When I load full page (not the part of it) - jQuery works as expected (that's why I came across the idea, that it needs to be "rebinded").
感谢您!
推荐答案
所以,如果我理解你的问题正确,你已经包含在HTML变量中一些JavaScript?如果是的话,将无法正常工作,因为这是从一个AJAX命中检索JavaScript是不是由浏览器因安全隐患执行。
So if I understand your question correctly you have some JavaScript contained within the html variable ? If so it will not work, because JavaScript that is retrieved from an AJAX hit is not executed by the browser due to security risks.
我建议你包括必要的JavaScript code在你的网页被发起Ajax请求,这样,当你添加新的内容就已经可用。
I recommend you include the necessary javascript code in your page that is initiating the the Ajax request so that it is already available when you append the new content.
*编辑...
monksp加入注释,说明如何让jQuery的做你想要什么有很大的联系。
monksp added a great link as a comment that shows how to have jQuery do exactly what you want.
下面的也有一些code做相同的,但手动:
Here's also some code to do the same but manually:
<html>
<head>
<title>Test JavaScript JSON</title>
</head>
<script src="https://www.google.com/jsapi"></script>
<script>
google.load('jquery', '1.3.2');
</script>
<body>
<script type="text/javascript">
$(document).ready(function() {
$.getJSON('testjs.json', function(json){
$(document.body).append(json.html);
eval(json.js);
});
});
</script>
</body>
</html>
下面是testjs.json的内容:
Here's the content of testjs.json:
{"html":"<p class=\"newelement\">Click me</p>","js":"$(\".newelement\").click(function() { alert($(this).text()); });"}
最后还有一堆existings插件和其他的东西来动态包含的JavaScript。我用锐获取了过去: http://developer.yahoo.com/yui/3/ GET /
这篇关于内联jQuery脚本AJAX调用中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!