简体   繁体   English

加载html后如何加载js文件?

[英]How to load a js file after loading html?

After i load this : 我加载后:

$('#buy-div').load('../buyBar/buyBar.html',function() {
 //do some things here
 });

I would like to include this : 我想包括以下内容:

<script src="../buyBar/BuyBar.js"></script> //access some html that does not exist

Beacuse in this js i am looking for some html that does not exist only after the .load function is done. 因为在此js我正在寻找仅在.load函数完成后才存在的一些html (such as getElementById or $('input').keyup(function() { that happens before the .load was finished. (例如getElementById$('input').keyup(function() {.load完成之前发生。

Just put the code that you want to run after the html is loaded in a function. 只需将要在HTML加载到函数中后运行的代码。 Then call that function in the callback function of the .load('../buyBar/buyBar.html') 然后在.load('../buyBar/buyBar.html')的回调函数中调用该函数

Assume "../buyBar/BuyBar.js" originally contains 假设“ ../buyBar/BuyBar.js”最初包含

document.getElementByID("#someElement").innerHTML = "...";

You can change it to 您可以将其更改为

function someFunction(){document.getElementByID("#someElement").innerHTML = "...";}

Now just put <script src="../buyBar/BuyBar.js"></script> in the <head> as usual. 现在,像往常一样,将<script src="../buyBar/BuyBar.js"></script>放在<head>中。 Then do this: 然后执行以下操作:

$('#buy-div').load('../buyBar/buyBar.html',function() {
    someFunction();
    //do other stuff   
});

I figure out i can do it by loading it when .load is done : 我发现我可以通过在.load完成后加载它来做到这一点:

     let script = document.createElement('script');
      script.src = "../buyBar/Pay.js";
      document.body.append(script);  

This works but i am not sure is the best solution. 这有效,但我不确定是最好的解决方案。

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

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