jquery - Read a text file?

前端 未结 2 1574
既然无缘
既然无缘 2020-11-27 18:29

I have an html file that I\'d like to open and read from, but I\'m not entirely sure how to do that... Basically, it\'s a fairly large file (big.html), and, in a separate fi

相关标签:
2条回答
  • 2020-11-27 18:29

    jQuery provides a method $.get which can capture the data from a URL. So to "read" the html/text document, it needs to be accessible through a URL. Once you fetch the HTML contents you should just be able to wrap that markup as a jQuery wrapped set and search it as normal.

    Untested, but the general gist of it...

    var HTML_FILE_URL = '/whatever/html/file.html';
    
    $(document).ready(function() {
        $.get(HTML_FILE_URL, function(data) {
            var fileDom = $(data);
            fileDom.find('h2').each(function() {
                alert($(this).text());
            });
        });
    });
    
    0 讨论(0)
  • 2020-11-27 18:51

    One solution would be to "read" in the 2nd document by putting it in a hidden iframe. Then you could access the HTML in that iframe as noted here and do whatever you like with that data.

    0 讨论(0)
提交回复
热议问题