我有以下的HTML代碼段獲取div標籤
<div id="column_1">
<div id="portlet_1">
<div id="portlet-header_1"> <input type="button" class="close_button"> </div>
<div id="portlet-sub-header_1"> sub header goes here </div>
<div id="portlet-content_1"> content goes here </div>
<div id="portlet-footer_1"> footer goes here </div>
</div>
<div id="portlet_2">
<div id="portlet-header_2"> <input type="button" class="close_button"> </div>
<div id="portlet-sub-header_2"> sub header goes here </div>
<div id="portlet-content_2"> content goes here </div>
<div id="portlet-footer_2"> footer goes here </div>
</div>
</div>
<div id="column_2">
<div id="portlet_3">
<div id="portlet-header_3"> <input type="button" class="close_button"> </div>
<div id="portlet-sub-header_3"> sub header goes here </div>
<div id="portlet-content_3"> content goes here </div>
<div id="portlet-footer_3"> footer goes here </div>
</div>
<div id="portlet_4">
<div id="portlet-header_4"> <input type="button" class="close_button"> </div>
<div id="portlet-sub-header_4"> sub header goes here </div>
<div id="portlet-content_4"> content goes here </div>
<div id="portlet-footer_4"> footer goes here </div>
</div>
</div>
的ID的如何:
得到列編號取決於被點擊其關閉按鈕?因此,如果在portlet-header_3中單擊了close_button,它應該返回column_2
根據單擊哪個關閉按鈕獲取portlet ID?所以,如果點擊close_button這是portlet的header_1,它應該返回portlet_1
我有以下,但返回最接近DIV,portlet的header_ *,這不是我所追求的:
$(".close_button").live('click', function(event) {
event.preventDefault();
var that = this;
alert($(that).closest("div").attr("id"))
});
當你創建按鈕時,你可以傳入哪個div來返回,並讓它成爲事件處理程序的一部分? –
@詹姆斯,聽起來像一個可怕的很多工作 – jcolebrand