剃刀/ HTML(很多這樣的):jQuery的每個onclick事件裏面
<div class="directory-item" id="@item_id" name="@item_name" style="padding-left: @(indent_level)px; display: @(display)">
<i class="fa @icon_type"></i>
@item.ItemName (id: @item_id, name: @item_name, level: @item.Level)
<i class="fa fa-angle-right float-right"></i>
</div>
的Javascript:
$(".directory-item").click(function() {
$('div[name*=' + this.id + ']').each(function() {
if (this.attr('display') == "none") {
this.show();
} else {
this.hide();
}
});
});
基本問題:你怎麼把一個 「每個」 內部的 「click」 事件並用「this」引用「each」項(每次訪問一個元素)?也許我不應該使用每一個陳述,而是其他的東西?
yes.you應該使用'this'或者甚至包含'$(this)'的jQuery包裝項目來訪問被單擊的項目。然後你可以使用'最近()','find'等方法訪問相關的項目。 – Shyju
你會如何使用find重寫上述內容?基本上,我試圖採用被點擊的div的id,並找到所有包含該id的名稱的div,然後顯示/隱藏切換它們。 – gunslingor
您不應該有多個具有相同Id值的項目!這是無效的HTML。你想達到什麼目的? – Shyju