jQuery->从其他属性获取id

时间:2012-04-23 18:07:46

标签: jquery

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

如果我的p_code为“WH15”,我需要返回“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>

1 个答案:

答案 0 :(得分:3)

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

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


但是:如果您可以更改标记,那么使用HTML5 data-* attributes会好得多。它们是allowed by the HTML5 specwork automagically with .data()