0

firePath の助けを借りて、私はこれを得ました:

.//*[@id='#table-row-51535240d7037e70b9000062']/td[1]

My HTML のパロットは次のようになります。

  <table class="table table-bordered table-striped">
    <tbody>
      <tr>
      <tr>
       <tr id="#table-row-51535240d7037e70b9000062"> #this is the id that i want to get
      <td>             54           </td>            #this is the td that i know
      <td>
      <td>
       <td>Open</td>
    <td/>

ここで本当にやりたいことは、td 値 (54) を指定することで、ID を取得 (ID を解析) できるようにしたいのですが、どうすればそれを達成できるのでしょうか?

前もって感謝します。

PS: 私の英語力と知識不足で申し訳ありません :)

4

2 に答える 2

1

まず、HTML が無効です (ネストされた<tr>ノードが含まれているため)。Nokogiri は解析できるかもしれませんが、できるならその前に修正する必要があります。

次の Ruby コードでその ID を取得できます。

doc.at_xpath("//td[contains(text(), '54')]/..")['id']
  1. //td[contains(text(), '54')]を含むすべての<td>ノードを取得し、それらの親に移動します。54/..
  2. Document#at_xpath最初に一致したアイテムのみを取得します
  3. ['id']一致するノードの属性を取得します。
于 2013-03-28T11:32:21.977 に答える
-1

jqueryの使用

$(function(){
    // (i dont know if you have id for that td or not, it will be more easy if u do have id for that td)
    console.log($('table tbody tr td:first').closest('tr').attr('id')); // you can remove :first if you want to.
});

おっと、私はあなたの質問を読み違えました。もう 1 つ、tr タグに問題があります。

于 2013-03-28T11:39:10.990 に答える