简体   繁体   English

如何确保加载的文件可以被js代码检测到

[英]How to make sure that the loaded file can be detected by a js code

I have this code in the file assets/URL.js :我在文件assets/URL.js中有这段代码:

$(function(){
    $("a").on("click", function () {
        event.preventDefault();
        var TheURL = $(this).attr('href');
        if(TheURL){
            $("#MyDiv").load(TheURL , { name: '' }, 
                function() {
                    alert("done");
                });
        }
    });
});

And what it does is that if an <a> element is clicked, the function above prevents the page from going to that page then it gets the url and load it into the div with the id MyDiv .它的作用是,如果单击<a>元素,上面的 function 会阻止页面转到该页面,然后它会获取 url 并将其加载到具有 id MyDivdiv中。

Now this is the code of index.html :现在这是index.html的代码:

<html>
  <head>
    <script type="text/javascript" src="assets/URL.js?v=1"></script>
  </head>
  <body>
    <div id="MyDiv">
      <a href="files/1.html"></a>
    </div>
  </body>
</html>

and the code of files/1.html is: files/1.html的代码为:

<a href="2.html">2</a>

now, when I click on <a> in the file index.html the content of the <div> with the id MyDiv changes the the content of file/1.html without the page reloading.现在,当我单击文件index.html中的<a>时,ID 为MyDiv<div>的内容会更改file/1.html的内容,而无需重新加载页面。

But when I click <a> in the file file/1.html the page reloads and goes to file/2.html and doesn't follow the code in assets/URL.js , how can I force it to follow it and just replace the content of the <div> with the id MyDiv ?但是,当我在文件file/1.html中单击<a>时,页面会重新加载并转到file/2.html并且不遵循assets/URL.js中的代码,我该如何强制它遵循它并且只是用 id MyDiv替换<div>的内容?

You need to setup the eventhandlers again when the dynamic html is loaded, right now you only bind the click event to the existing A tags in index.html, not the future ones in dynamic loaded content like 1.html加载动态 html 时,您需要再次设置事件处理程序,现在您只需将点击事件绑定到 index.html 中现有的 A 标签,而不是动态加载内容中的未来事件,如 1.ZFC35FDC70D5FC69D253EZAC788

You could try something like:您可以尝试以下方法:

$(function(){
  RefreshEventHandlers();
});

function RefreshEventHandlers() {
    $("a").on("click", function () {
        event.preventDefault();
        var TheURL = $(this).attr('href');
        if(TheURL){
            $("#MyDiv").load(TheURL , { name: '' }, 
                function() {
                    alert("done");
                    RefreshEventHandlers(); // bind click to newly added A tags.
            });
        }
    });
}

To make sure the click event handlers are set on all new A tags确保在所有新的 A 标记上设置单击事件处理程序

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

相关问题 在提交表单之前,如何确保重新验证码js文件已加载? - How to make sure that the re-captcha js file has been loaded before submitting a form? 如何确保在页面继续之前加载一些js文件 - How can i make sure that some js files are loaded before the page continue 如何确保在Meteor中的其他文件之前加载Three.js? - How can I make sure Three.js is loaded before other files in Meteor? 如果尚未从服务器检索到另一个js文件,如何确保可以从另一个js文件中加载代码? - How do I make sure I can load code from a another js file in case it wasn't retrieved from the server yet? 如何确保正确加载geojson文件? - How can I make sure that the geojson files are loaded properly? 在调用另一个函数之前,请确保已加载JS文件并调用了JS函数 - Make sure JS file is loaded and JS function is called BEFORE calling another function 如何确保在NodeJS中加载了模块 - How to make sure a module is loaded in NodeJS 如何确保图像已完全加载到DOM中 - How to make sure that an image is fully loaded in the DOM 如何使我的.css和.js文件与我在Android上的WebView中加载的html代码一起使用 - How to make my .css and .js file work with my html code loaded in a WebView on Android 如何确保在调用js函数之前将动态生成的div加载到DOM中? - How do I make sure dynamically generated div is loaded into DOM before js function is called on it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM