简体   繁体   English

如何使用JQuery在嵌套HTML中提取文本?

[英]How to extract text inside nested HTML using JQuery?

I have here HTML Code: 我这里有HTML代码:

<div class="actResult" style="border: solid">
    <table>
        <tbody>
            <tr>
                <td>Order Number</td>
                <td>1</td>
            </tr>
            <tr>
                <td>Customer Number</td>
                <td>3</td>
            </tr>
            <tr>
                <td>Complaint Code</td>
                <td>b</td>
            </tr>
            <tr>
                <td>Receivable Receipt Number</td>
                <td>5</td>
            </tr>
            <tr>
                <td>Date Called</td>
                <td>2014-03-19</td>
            </tr>
            <tr>
                <td>Scheduled Day Of Checkup</td>
                <td>2014-03-19</td>
            </tr>
            <tr>
                <td>Scheduled Day Of Service</td>
                <td>2014-03-21</td>
            </tr>
            <tr>
                <td>Checkup Status</td>
                <td>Y</td>
            </tr>
            <tr>
                <td>Service Status</td>
                <td>N</td>
            </tr>
            <tr>
                <td>Technician Number Checkup</td>
                <td>3</td>
            </tr>
            <tr>
                <td>Technician Number Service</td>
                <td>1</td>
            </tr>
        </tbody>
    </table>
</div>

I want to get the values of the tags and put them into an array with the a structure like array("first td" => "second td"), so for this case the array would be array("Order Number" => "1", "Customer Number" => "3", "Complaint Code" => "b", ...) and so on. 我想获取标签的值并将其放入具有array(“ first td” =>“ second td”)这样的结构的数组中,因此在这种情况下,数组将为array(“ Order Number” => “ 1”,“客户编号” =>“ 3”,“投诉代码” =>“ b”,...)等。

After that, the final array would be sent into a PHP code. 之后,最后的数组将被发送到PHP代码中。

I've been trying to extract some of the values from the HTML using var html = $(this).filter(function( index ){ return $("td", this) }).filter(":odd").text(); 我一直在尝试使用var html = $(this).filter(function( index ){ return $("td", this) }).filter(":odd").text();从HTML提取一些值var html = $(this).filter(function( index ){ return $("td", this) }).filter(":odd").text(); and various other combinations of filter(), but it doesn't seem to work for me. 和filter()的其他各种组合,但对我来说似乎不起作用。

How do I go about doing what I want to do? 我该如何去做自己想做的事?

jsFiddle Demo

You are going to want to use .each for that and iterate through the rows in the table. 您将要为此使用.each并遍历表中的行。 For each row, take the first cell ( .eq(0) ) as the key, and the second cell ( .eq(1) ) as the value. 对于每一行,将第一个单元格( .eq(0) )作为键,将第二个单元格( .eq(1) )作为值。 Place these in a result object. 将它们放在结果对象中。

//object to hold resulting data
var result = {};

//iterate through rows
$('.actResult tr').each(function(){

 //get collection of cells
 var $tds = $(this).find('td');

 //set the key in result to the first cell, and the value to the second cell
 result[$tds.eq(0).html()] = $tds.eq(1).text();
});

You can get the rows property of the table element and create an object based on the cells' value: 您可以获取table元素的rows属性,并根据单元格的值创建一个对象:

var rows = document.querySelector('.actResult table').rows, 
    data = {}, c, l = rows.length, i = 0;

for (; i < l; i++) {
    c = rows[i].cells;
    data[c[0].innerHTML] = c[1].innerHTML;
}

http://jsfiddle.net/tG8F6/ http://jsfiddle.net/tG8F6/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM