2014-04-20 15 views
0

我試圖做到這一點,當我點擊退出按鈕,它通過父母並選擇包裝類,然後刪除並添加一個類。父 - jquery

HTML

<div class="app-icon-container"> 
    <div class="app-icon-wrapper active"> 
      <img class="app-icon" src="http://www.antivirus-blog.com/wp-content/uploads/2014/02/google-chrome-logo.png"> 
    </div> 
    <div class="app-hover-window"> 
      <div class="exit-btn"></div> 
    </div> 
</div> 

JQuery的

$('.exit-btn').click(function() { 
     $(this).closest('.app-icon-wrapper').removeClass('active').addClass('un-active'); 
    }); 
+0

正確關閉img標籤。它應該是。最後注意「/>」。 –

+0

@KamleshKushwaha - 在HTML5中可選 – adeneo

+0

@adeneo好的,謝謝,不知道 –

回答

0

.app-icon-wrapper不是.exit-btn父母,所以closest()將無法​​正常工作,它僅適用於父母。
你可以得到的容器而不是,然後找到包裝

$('.exit-btn').on('click', function() { 
    $(this).closest('.app-icon-container') 
      .find('.app-icon-wrapper') 
      .removeClass('active') 
      .addClass('un-active'); 
}); 

或類似的東西(而不是動態)

$('.exit-btn').on('click', function() { 
    $(this).parent().prev().removeClass('active').addClass('un-active'); 
}); 
0

如果出口BTN是withing你的包裝,你可以使用

$(this).parents('.app-icon-wrapper').remove...... 

但在你的情況在這裏

$(this).parent().find('.app-icon-wrapper').remove.... 
0

將它更改爲以下代碼:

$('.exit-btn').click(function() { 
    $(this).parent().closest('.app-icon-wrapper').removeClass('active').addClass('un-active'); 
}); 

因爲你要引用的元素是當前元素的父兄弟。本身不是。