如何从Javascript中解析外部文件中的html

时间:2022-07-05 06:13:47

I feel like I should know this, but as I'm sitting down trying to access an html file from my Javascript I don't really know where to start. I am using jQuery so do I just want to use jQuery.ajax()?

我觉得我应该知道这一点,但是当我坐下来尝试从我的Javascript访问一个html文件时,我真的不知道从哪里开始。我正在使用jQuery所以我只想使用jQuery.ajax()?

The external html is a table with various premium values, stored on the same domain but in a separate directory. When a user enters their birth year previously in the form and selects whether or not they are male or female and a smoker or non-smoker, I return specific values based on their age.

外部html是一个具有各种高级值的表,存储在同一个域中,但存储在单独的目录中。当用户先前在表格中输入他们的出生年份并选择他们是男性还是女性以及吸烟者还是非吸烟者时,我会根据他们的年龄返回特定值。

In the table below if they were 21, male, non-smoker i'd want to return the value in the mns cell from row #id21.

在下表中,如果他们是21岁,男性,非吸烟者我想要从行#id21返回mns单元格中的值。

<table id="premiumRateTable" cellpadding="0" cellspacing="0" border="0">
    <thead>
        <tr>
            <th id="age">Age Next Birthday</th>
            <th id="cover">Default Cover</th>
            <th id="mns">Male Non-smoker</th>
            <th id="ms">Male Smoker</th>
            <th id="fns">Female Non-smoker</th>
            <th id="fs">Female Smoker</th>
        </tr>
    </thead>
    <tbody>
        <tr id="id20">
            <td headers="age">20</td>
            <td headers="cover">$100,000</td>
            <td headers="mns">$108.99</td>
            <td headers="ms">$154.55</td>
            <td headers="fns">$44.31</td>
            <td headers="fs">$68.61</td>
        </tr>
        <tr id="id21">
            <td headers="age">21</td>
            <td headers="cover">$150,000</td>
            <td headers="mns">$160.81</td>
            <td headers="ms">$229.15</td>
            <td headers="fns">$58.16</td>
            <td headers="fs">$77.48</td>
        </tr>
        <tr id="id22">
            <td headers="age">22</td>
            <td headers="cover">$150,000</td>
            <td headers="mns">$139.37</td>
            <td headers="ms">$199.167</td>
            <td headers="fns">$58.28</td>
            <td headers="fs">$72.89</td>
        </tr>
        <tr id="id23">
            <td headers="age">23</td>
            <td headers="cover">$150,000</td>
            <td headers="mns">$128.64</td>
            <td headers="ms">$183.59</td>
            <td headers="fns">$56.28</td>
            <td headers="fs">$72.89</td>
        </tr>
        <tr id="id24">
            <td headers="age">24</td>
            <td headers="cover">$150,000</td>
            <td headers="mns">$121.94</td>
            <td headers="ms">$172.87</td>
            <td headers="fns">$58.29</td>
            <td headers="fs">$79.90</td>
        </tr>
        <tr id="id25">
            <td headers="age">25</td>
            <td headers="cover">$150,000</td>
            <td headers="mns">$112.56</td>
            <td headers="ms">$158.13</td>
            <td headers="fns">$61.11</td>
            <td headers="fs">$84.91</td>
        </tr>

    </tbody>
</table>    

1 个解决方案

#1


3  

Load the table once ($(document).ready()) and add it to an invisible element at the end of the body.

加载表一次($(document).ready())并将其添加到正文末尾的不可见元素。

var dummyDiv = $("<div id='dummyDiv'/>").css("display","none");
dummyDiv.load("otherpage.html #id"+age);
dummyDiv.css("display","none");
$("body").append(dummyDiv);

And then you can run this code from anywhere to get the values from that table.

然后,您可以从任何地方运行此代码以从该表中获取值。

var dummyDiv = $("#dummyDiv");
var valYouWant = "mns"; //example value to grab
var value = dummyDiv.find('td[headers="'+valYouWant+'"]').text();
//do whatever you'd like with 'value'

#1


3  

Load the table once ($(document).ready()) and add it to an invisible element at the end of the body.

加载表一次($(document).ready())并将其添加到正文末尾的不可见元素。

var dummyDiv = $("<div id='dummyDiv'/>").css("display","none");
dummyDiv.load("otherpage.html #id"+age);
dummyDiv.css("display","none");
$("body").append(dummyDiv);

And then you can run this code from anywhere to get the values from that table.

然后,您可以从任何地方运行此代码以从该表中获取值。

var dummyDiv = $("#dummyDiv");
var valYouWant = "mns"; //example value to grab
var value = dummyDiv.find('td[headers="'+valYouWant+'"]').text();
//do whatever you'd like with 'value'