且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

jQuery的AJAX的load()Java脚本不执行?

更新时间:2023-12-04 18:46:58

如果我理解你正确地问什么......

If I understood what you asked correctly...

您想通过Ajax加载到你的页面上一个div,让我们把它叫做;

you want to load via ajax into a div on your page, lets call it;

1)DIV ID =loadStuffHere的(abc.html)

1) div id="loadStuffHere" of (abc.html)

2)的东西来填充loadStuffHere来自xyz.html

2) the stuff to populate "loadStuffHere" comes from xyz.html

因此​​,只要做到这一点;

So just do this;

$(loadStuffHere)的负载(xyz.html);

$("loadStuffHere").load("xyz.html");

但等待!你不希望有加载一切从xyz.html你只是要加载的xyz.html的一部分说(xyz.html)

BUT WAIT!! You dont want to have to load everything from xyz.html you just want to load a portion of xyz.html say of (xyz.html)

因此​​,只要做到这一点;

So just do this;

$(loadStuffHere)的负载(xyz.html #loadMeOnly);

$("loadStuffHere").load("xyz.html #loadMeOnly");

但等待!比方说DIV ID =loadMeOnly里面是一个手风琴等等,这意味着它已经被初始化,这意味着我只好还包括JavaScript的......嗯,怎么办...

BUT WAIT!! Lets say inside of div id="loadMeOnly" is an accordion so which means it has to be initialized which means u have to also include the javascripts... hmm, what to do...

您可能会认为这样做的;

You would think of doing this;

$(loadStuffHere)的负载(xyz.html #loadMeOnly)。   $ .getScript('JS / xyz.js');

$("loadStuffHere").load("xyz.html #loadMeOnly"); $.getScript('js/xyz.js');

好了上面的吮吸,因为一)U将需要创建一个外部js文件和b)你实际上是使2 HTTP调用,当u可以用​​1 HTTP调用如果通过正常的非Ajax的方式做的做到这一点。

Well the above sucks because a) u would need to create an external js file and b) You are actually making 2 http calls, when u could do it with 1 http call if you did it by normal non-ajax way.

所以......***的解决办法是让2东西拨打(HTML和JS的 - 1号线,1 HTTP)这是ü如何做到这一点;

So... the best solution is to get 2 things with 1 call the (HTML and the js - 1 line, 1 http) here is how u do it;

$(loadStuffHere)的负载(xyz.html #loadMeOnly,剧本);

$("loadStuffHere").load("xyz.html #loadMeOnly, script");

这将加载#loadMeOnly DIV和所有的脚本标记,你还是你会拥有这一切...开心快乐:)

This loads the #loadMeOnly div AND all script tags, so u would have it all... happy happy :)

这里的窍门是逗号......ü可以挑选加载任何延迟性肌肉酸痛ü要

the trick here is commas... u could pick and choose to load whatever doms u want