我有一個使用Javascript的自定義手風琴設置。目前正在使用onclick,我將一個樣式切換到父div(當前擁有一個「卡」類並添加「card-open」)。我的問題是如何更高一格,並開始操縱類「時間蒸汽事件」的div?如何在javascript中選擇父div的父項?
活生生的例子:http://bit.ly/1rr6zGA
HTML:
<div class="time-stream-event" id="{entry_id}">
<div style="background: url({timeline_item_img}) no-repeat center center;" class="event-bg grayscale"></div>
<div class="card">
<h3><a href="{title_permalink="/timeline"}">{title}</a></h3>
<div class="event-year">{timeline_item_date format="%F %Y"}</div>
<a href="#" class="togglelink"></a>
<div class="toggle-box">
<p>{timeline_item_description}</p>
<div class="employees">Number of Employees</div>
<div class="employee-count-no"> <i class="ss-icon">👥</i> {timeline_item_employees}</div>
</div>
<div class="open-arrow"></div>
</div>
</div>
的Javascript:
$.fn.slideFadeToggle = function(speed, easing, callback) {
return this.animate({opacity: 'toggle', height: 'toggle'}, speed, easing, callback);
};
$('.toggle-box').hide();
$('a.togglelink').on('click', function (e) {
e.preventDefault();
var elem = $(this).next('.toggle-box')
$('.toggle-box').not(elem).hide();
elem.slideFadeToggle();
var parent_div = $(this).parent().toggleClass('card-open');
});
嘗試'$(this).closest('。time-stream-event');' – mdesdev
嘗試.parentElement它會根據您的DOM獲取直接父級。 – ambarox
document.getElementsByClassName(「togglelink」)[0] .parentElement; – ambarox