jquery检查来自另一个页面的h1是否有单词“stock”

时间:2015-07-02 19:09:35

标签: javascript jquery html

我正在努力从另一页读取H1。我需要阅读的页面中的H1是动态的,我需要检查它是否有特定的单词,如:

  

如果h1有单词“stock”,则运行'js / stock.js',否则如果h1有   单词“formula”,运行'js / formula.js',否则如果h1有单词“tour”,   运行'js / tour.js'。

我只能找到关于从其他页面h1到我的页面加载内容的代码,但不能读取。

我需要阅读的页面中的HTML代码:

<h1 id="Title"><img src="http://www.ntcrono.com/nt/Images/_Stop.png">Camp. Bras. Stock Car - 2ª Bateria<img src="http://www.ntcrono.com/nt/Images/_Stop.png"></h1>
<p id="Time"><span id="DayTime">26/4/2015 14:49:40</span><span id="ElapsedTime">00:34:42</span><span id="RemainingTime">0 Lp., </span></p>

<table width="100%">
<tr class="HeaderRow">
<td id="Id_Position" align="left" width="25">P</td>
<td id="Id_Numero" align="left" width="25">No.</td>
<td id="Id_Perso1" align="left" width="80">Driver</td>
<td id="Id_Equipe" align="left" width="190">Team</td>
<td id="Id_Vehicule" align="left" width="60">Model</td>
<td id="Id_NbTour" align="left" width="25">L</td>
<td id="Id_TpsCumule" align="left" width="85">Total time</td>
<td id="Id_VitesseMoyTotal" align="left" width="30">Avg</td>
<td id="Id_Ecart1er" align="left" width="35">Gap</td>
<td id="Id_EcartPrec" align="left" width="50">PGap</td>
<td id="Id_MeilleurTour" align="left" width="50">Best</td>
<td id="Id_AuTour" align="left" width="25">In</td>
<td id="Id_Inter1" align="left" width="45">S1</td>
<td id="Id_MeilleurInter1" align="left" width="45">BS1</td>
<td id="Id_Inter2" align="left" width="45">S2</td>
<td id="Id_MeilleurInter2" align="left" width="45">BS2</td>
<td id="Id_Inter3" align="left" width="45">S3</td>
<td id="Id_MeilleurInter3" align="left" width="45">BS3</td>
<td id="Id_TpsTour" align="left" width="50">Laptime</td>
<td id="Id_NbStand" align="left" width="20">P</td>
<td id="Id_PositionDepart" align="left" width="20">StartP</td>
</tr>
<tr class="OddRow">
<td align="left" class="">1</td>
<td align="left" class="">65</td>
<td align="left" class="">MAX</td>
<td align="left" class="">Eurofarma RC</td>
<td align="left" class="">Chevrolet</td>
<td align="left" class="">26</td>
<td align="left" class="">00:28:06.567</td>
<td align="left" class="">126.42</td>
<td align="left" class="">-</td>
<td align="left" class="">-</td>
<td align="left" class="">55.487</td>
<td align="left" class="">23</td>
<td align="left" class="BestTime">15.572</td>
<td align="left" class="">15.572</td>
<td align="left" class="">-</td>
<td align="left" class="">16.086</td>
<td align="left" class="">21:55.535</td>
<td align="left" class="">24.066</td>
<td align="left" class="">56.208</td>
<td align="left" class="">0</td>
<td align="left" class="">1</td>
</tr>
<tr class="EvenRow">
<td align="left" class="">2</td>
<td align="left" class="">0</td>
<td align="left" class="">CBU</td>
<td align="left" class="">Red Bull Racing</td>
<td align="left" class="">Chevrolet</td>
<td align="left" class="">26</td>
<td align="left" class="">00:28:07.910</td>
<td align="left" class="">126.32</td>
<td align="left" class="">1.343</td>
<td align="left" class="">1.343</td>
<td align="left" class="">55.590</td>
<td align="left" class="">10</td>
<td align="left" class="">15.977</td>
<td align="left" class="">15.711</td>
<td align="left" class="">-</td>
<td align="left" class="">16.126</td>
<td align="left" class="">6:57.520</td>
<td align="left" class="">24.183</td>
<td align="left" class="">56.293</td>
<td align="left" class="">0</td>
<td align="left" class="">6</td>
</tr>
</table>

1 个答案:

答案 0 :(得分:1)

使用$.get将其他网站的内容转换为变量。然后解析它并检查<h1>

的内容
$.get(otherSiteUrl, function(data) {
    var dom = $('<div>', {html: data }); // parse the HTML
    var h1Text = dom.find("h1").text();
    if (h1Text.indexOf("Stock") != -1) {
        $.getScript("js/stock.js");
    } else if (h1Text.indexOf("Formula") != -1) {
        $.getScript("js/formula.js");
    } else if (h1Text.indexOf("Tour") != -1) {
        $.getScript("js/tour.js");
    }
}, "html");