简体   繁体   English

在使用jQuery Ajax加载的页面中运行javascript

[英]Running javascript in page that is loaded with jQuery Ajax

I have a function that loads parts of other pages into my div #ajaxloadconent, here is the function: 我有一个函数将其他页面的部分加载到我的div #ajaxloadconent中,这里是函数:

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

However, the problem is that I am unable to run javascript on the page that I load, while using this function. 但是,问题是我无法在我加载的页面上运行javascript,同时使用此功能。 I simply want to run this: 我只是想运行这个:

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

When the data is loaded through .load(). 通过.load()加载数据时。 The script is contained on the page that I load, so I do not want to modify the .load() function that I use. 该脚本包含在我加载的页面中,因此我不想修改我使用的.load()函数。 If anyone knows how to fix this, then help would be greatly appreciated. 如果有人知道如何解决这个问题,那么非常感谢帮助。 I can also provide further information if necessary. 如有必要,我还可以提供进一步的信息。

Try to use this 试着用这个

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

from the jQuery docs : 来自jQuery文档

"html": Returns HTML as plain text; “html”:以纯文本形式返回HTML; included script tags are evaluated when inserted in the DOM. 包含的脚本标记在插入DOM时进行评估。

Or you can use http://api.jquery.com/jQuery.getScript if you seperate your script to a standalone js file 或者,如果将脚本分离到独立的js文件,则可以使用http://api.jquery.com/jQuery.getScript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM