简体   繁体   中英

how can i detect data from an external file using javascript?

I usually use "getElementById()" to detect internal file data . I tried this to detect data from an external file but its not work :

<script>
var outterPage ="dom1.html";
var temp=outterPage.getElementById("abc").innerHTML;
</script>

what can i do to detect data from external file ?

You can use the jQuery function $.get to read the contents of a URL into a variable. Then you can parse that into a DOM element to find parts of it.

$.get("dom1.html", function(data) {
    var temp = $("<div>", { html: data }).find("#abc").html();
    // do something with temp
});

You can also specify the ID in the URL argument to $.get , so jQuery will just return the contents of that part of the page.

$.get("dom1.html #abc", function(temp) {
    // do something with temp
});

You can use jquery .load() to load the external file into a dom element and then get its value.

<script>
$(document).ready(function(){
$("#holder").load("dom1.html");
var temp=$("#holder").find("#abc").innerHTML;
});
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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