Aar*_*den 0 jquery dom removeclass
我今天早上似乎遇到了一些麻烦removeClass.首先我单击a div添加Class 'containerhover',然后当我单击关闭按钮时,它不会删除它刚刚添加到的类div.
我不知道这是否与它仅被添加到dom中并且实际上没有被编码到HTML中有关.请有人帮忙.
HTML
<div class="container"></div>
Run Code Online (Sandbox Code Playgroud)
jQuery的
$(function() {
$('.container').click(function(e) {
var mythis = $(this);
mythis.addClass("containerhover");
mythis.find('.backface').addClass("bfhover");
mythis.find('.album').addClass("ahover");
});
$('.closealbum').click(function(e) {
$('.container').removeClass('containerhover');
});
});
Run Code Online (Sandbox Code Playgroud)
使用.on(),将事件附加到容器,并仅在.closealbum单击元素时激活它.另外,添加event.stopPropagation()方法以防止事件进一步冒泡:
$('.container').on('click', '.closealbum', function(e) {
$('.container').removeClass('containerhover');
e.stopPropagation();
});
Run Code Online (Sandbox Code Playgroud)
.closealbum绑定click事件时元素不存在的事实引起的.e.stopPropagation(),事件会冒泡,第一个事件也会被触发,撤消第二个事件的removeClass.| 归档时间: |
|
| 查看次数: |
1921 次 |
| 最近记录: |