jquery - 读取文本文件? [英] jquery - Read a text file?
问题描述
$(document).ready(function(){
$(h2)。each(function(){
var title = $(this).text();
$(#mydiv)。append(title);
});
});
...
< div id =mydiv>< / div>
我有点困惑如何使用jquery ...我很新整个事情,所以我甚至不确定这是可能的...
jQuery提供了一个方法 $。get
可以从URL捕获数据。所以为了阅读html / text文档,需要通过一个URL来访问它。一旦获取了HTML内容,您应该可以将该标记作为jQuery包装集合进行包装,并按正常方式进行搜索。
未经验证,但是它的一般要点。 ..
var HTML_FILE_URL ='/whatever/html/file.html';
$(document).ready(function(){
$ .get(HTML_FILE_URL,function(data){
var fileDom = $(data);
每个(function(){
alert($(this).text());
});
});
} );
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 file (titles.html), I have some jquery code that I'd like to use to find certain elements (namely, h2 tags) and get the inner text from those tags and write just that text to titles.html... I'm not sure, particularly, how to open a separate file and read from it, and secondly, I'm not sure if the code below will work when it comes to getting the text within the h2 tags...
$(document).ready(function() {
$("h2").each(function() {
var title = $(this).text();
$("#mydiv").append(title);
});
});
...
<div id="mydiv"></div>
I'm a bit confused how to do that with jquery... I'm pretty new to the whole thing, so I'm not even sure it's possible...
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());
});
});
});
这篇关于jquery - 读取文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!