![](/img/trans.png)
[英]How to get the ID value of a parent element by class that is not immediate? jQuery
[英]How to get ID value of first element with specific class in html by jQuery
我有這個HTML代碼:
<div id="calc_pol_list">
<li class="calc_li">
<span class="right_list_number">1</span>
<p id="120" class="calc_p"/>
</li>
<li class="calc_li">
<span class="right_list_number">2</span>
<p id="100" class="calc_p"/>
</li>
</div>
我需要獲得第一個p的ID:使用jQuery並將此ID轉換為字符串。
我試過這段代碼:
$('#calc_pol_list p:first').attr('id');
但它不起作用。
我怎樣才能做到這一點?
更新:這是我的功能:
function refreshCost(){
if ( jQuery.isReady ) {
stomCost = parseInt($('div#calc_pol_list calc_p:first').attr('id'));
var cost = stomCost + scatCost + polCost;
$("#pre_cost").text(cost);
};
};
是的,我的代碼是正確的,如果與其余代碼分開考慮。 我意識到問題所在,但我不知道如何解決它。 問題是這些
我在Ajax請求之后添加了不在其原始DOM中的內容。 形式,擁有這些
在div里面。 我在這里讀到,我需要讓形式成為身體的直接孩子,但在這種情況下,這是不可能的。
$('#calc_pol_list p').first().attr('id')
像這樣 :)
解決方案是使用.ajaxStop
$(document).ajaxStop(function() {
alert($('#calc_pol_list p').eq(0).attr('id'));
});
嘗試這個:
$(document).ready(function(){
alert($('#calc_pol_list p:first').attr('id'));
});
ID和NAME令牌必須以字母([A-Za-z])開頭,后面可以跟任意數量的字母,數字([0-9]),連字符(“ - ”),下划線(“_”) ,冒號(“:”)和句號(“。”)。
請參閱: HTML中id屬性的有效值是什么?
創建一個變量並在其中獲取變量,例如
var content = $('#calc_pol_list > li p:first').attr('id');
並提醒它你將獲得id作為字符串,例如
alert (content );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.