0

在下面的 HTML 中,我需要根据 p_code 属性检索 p_id 属性。例如:

如果我有“WH15”的 p_code,我需要返回“63”。

我将如何使用 jQuery 来做到这一点?

<div id="deslist_13" class="sch_accordion_item_content_active" style="display: block;">
    <div class="left_shadow"></div>
    <div p_code="BRE" id="pdiv_60" p_id="60" class="program odd">
      <input type="hidden" id="hidden_60" value="BRE">
      <input type="hidden" id="tabId_60" value="13">
      <div class="cb"></div><span>BRE</span>
    </div>
    <div p_code="WH13" id="pdiv_61" p_id="61" class="program">
      <input type="hidden" id="hidden_61" value="WH13">
      <input type="hidden" id="tabId_61" value="13">
      <div class="cb"></div><span>WH13</span>
    </div>
    <div p_code="WH14" id="pdiv_62" p_id="62" class="program odd">
      <input type="hidden" id="hidden_62" value="WH14">
      <input type="hidden" id="tabId_62" value="13">
      <div class="cb"></div><span>WH14</span>
    </div>
    <div p_code="WH15" id="pdiv_63" p_id="63" class="program">
      <input type="hidden" id="hidden_63" value="WH15">
      <input type="hidden" id="tabId_63" value="13">
      <div class="cb"></div><span>WH15</span>
    </div>
    <div p_code="BPA" id="pdiv_64" p_id="64" class="program odd">
      <input type="hidden" id="hidden_64" value="BPA">
      <input type="hidden" id="tabId_64" value="13">
      <div class="cb"></div><span>BPA</span>
    </div>
    <div p_code="BPR" id="pdiv_65" p_id="65" class="program">
      <input type="hidden" id="hidden_65" value="BPR">
      <input type="hidden" id="tabId_65" value="13">
      <div class="cb"></div><span>BPR</span>
    </div>
</div>
4

1 回答 1

3
var p_id = $('div[p_code="WH15"]').attr('p_id');

演示:http: //jsfiddle.net/mattball/9Htdq/


但是:如果您可以更改标记,那么使用HTML5data-*属性会好得多。它们被HTML5 规范所允许,并自动与.data().

于 2012-04-23T18:10:04.430 回答