需要用javascript获取数据 [英] need to get data with javascript

查看:81
本文介绍了需要用javascript获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面有这样的HTML。

可以有人帮忙,我不知道怎么做?

i需要使用javascript从html获取数据以下数据。

1:45 PM  ET,  2014年4月9日星期三

布希体育场,圣路易斯,



i have html like this below.
can anybody help, i am not getting any idea how to?
i need to get data below data from html with javascript.
1:45 PM ET, Wednesday, April 9, 2014
Busch Stadium, St. Louis,

<div class="sdi-quickhits">
      When: 1:45 PM ET, Wednesday, April 9, 2014<br/>Where: Busch Stadium, St. Louis, Missouri<br/>Temperature:
	60°
		<br/>Umpires: 
	Home -
			Dale Scott, 1B -
			Dan Iassogna, 2B -
			CB Bucknor, 3B -
			Hal Gibson III<br/>Attendance: 
		41137<br/></div>

推荐答案

请参阅我对该问题的评论。问题是:如果您的HTML与使用脚本的地方相同,则会立即对其进行解析,因此您只能使用Javascript操作DOM树。

基于此,答案33显示了您感兴趣的技巧可用于解析任何HTML字符串: http://stackoverflow.com/questions/10585029/ parse-a-html-string-with-js [ ^ ]。



此外,jQuery已经有一个明确的HTML解析器: http://api.jquery.com/jquery.parsehtml [ ^ ]。



或者,你可以找到一些其他可用的HTML解析器:http://bit.ly/1hmkFs2 [ ^ ]。



例如: https ://www.npmjs.org/package/htmlparser [ ^ ]。



此外,如果您确定HTML是用格式良好的XML文档编写的,您可以将其解析为XML:http://www.w3schools.com/xml/xml_parser.asp [ ^ ]。



-SA
Please see my comment to the question. The thing is: if your HTML is the same where your script is used, it is parsed immediately, so you only manipulate DOM tree with Javascript.
Based on that, the answer 33 shows interesting technique which you can use to parse any HTML string: http://stackoverflow.com/questions/10585029/parse-a-html-string-with-js[^].

Also, jQuery already has an explicit HTML parser: http://api.jquery.com/jquery.parsehtml[^].

Alternatively, you can find some other available HTML parser: http://bit.ly/1hmkFs2[^].

For example: https://www.npmjs.org/package/htmlparser[^].

Besides, if you are sure that the HTML is written as well-formed XML document, you can parse it as XML: http://www.w3schools.com/xml/xml_parser.asp[^].

—SA


试试这个。您必须稍微更改代码以删除时间:/ Where:/ br在最终文本中。但现在你知道了这个想法:)



Try this. You have to change the code little bit to remove When:/Where:/br in the final text. But now you know the idea :)

function search() {
        var searchValue1 = "When: ";
        var searchValue2 = "Where: ";
        var searchValue3 = "Temperature: ";

        var elements = document.querySelectorAll('.sdi-quickhits');
        var index1 = elements[0].innerHTML.indexOf(searchValue1);
        var index2 = elements[0].innerHTML.indexOf(searchValue2);
        var index3 = elements[0].innerHTML.indexOf(searchValue3);

        if (index1 > -1 && index2 > -1 && index3 > -1) {
            var res1 = elements[0].innerHTML.substring(index1, index2);
            var res2 = elements[0].innerHTML.substring(index2, index3);
            alert(res1);
            alert(res2);
        }
    }


这篇关于需要用javascript获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆