在jQuery Ajax加载的页面中运行javascript [英] Running javascript in page that is loaded with jQuery Ajax

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

问题描述

我有一个函数可以将其他页面的一部分加载到div #ajaxloadconent中,这是该函数:

I have a function that loads parts of other pages into my div #ajaxloadconent, here is the function:

$('#ajaxloadcontent').load(url+' #ajaxloadcontent');

但是,问题是,使用此功能时,我无法在加载的页面上运行javascript.我只想运行这个:

However, the problem is that I am unable to run javascript on the page that I load, while using this function. I simply want to run this:

<script type="text/javascript">
document.title = 'PearlSquirrel About';
</script>

通过.load()加载数据时.该脚本包含在我加载的页面上,因此我不想修改我使用的.load()函数.如果有人知道如何解决此问题,将不胜感激.如有必要,我还可以提供更多信息.

When the data is loaded through .load(). The script is contained on the page that I load, so I do not want to modify the .load() function that I use. If anyone knows how to fix this, then help would be greatly appreciated. I can also provide further information if necessary.

推荐答案

尝试使用此

$.ajax({
  url: url,
  dataType: 'html',
  success: function(data) {
    //Do something with the data e.g.
    $(data).find("#ajaxloadedcontent").appendTo("#ajaxloadcontent");
  }
});

来自 jQuery文档:

"html":以纯文本形式返回HTML;评估包含的脚本标签 当插入DOM时.

"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

或者您可以使用 http://api.jquery.com/jQuery.getScript 您可以将脚本分离到一个独立的js文件中

Or you can use http://api.jquery.com/jQuery.getScript if you seperate your script to a standalone js file

这篇关于在jQuery Ajax加载的页面中运行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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