为什么悬停和单击不总是有效
why does the hover and click not always work?
这是我的小提琴
除了有时mouseenter、mouseleave、click函数(.item)并不总是有效之外,几乎可以完美地工作——并且需要单击它才能重新开始工作?为什么这个-这是我的代码-
$(document).ready(function () {
$('.timelineTile').click(function (evt) {
evt.stopPropagation();
$('.timelineTile').not(this).removeClass('clicked').find('.pull_down_content').height(0);
$(this).toggleClass('clicked');
if(!$('.timelineTile').hasClass("clicked")){
$(this).children('.pull_down_content').height(0);
}
}); });
$(document).click(function () {
$('.timelineTile').removeClass('clicked');
$('.pull_down_content').height(0);
$('.inner').stop().css({'display': 'none'}, 300);
});
$(document).ready(function () {
$('.timelineTile').children('.item').on('mouseenter mouseleave click', function(e) { e.stopPropagation();
if ($(this).parent('.timelineTile').hasClass("clicked")) {
if (!$(this).data('clicked')) {
var Height = e.type==='mouseenter' ? '90px' : e.type==='click' ? '250px' : '0px';
$(this).siblings('.pull_down_content').stop().animate({'height': Height}, 300);
$(this).siblings('.pull_down_content').children('.inner').css({'display': 'block'}, 300);
if (e.type==='click') $(this).data('clicked', true);
}else{
if (e.type==='click') {
$(this).data('clicked', false);
$(this).siblings('.pull_down_content').stop().animate({'height': '0px'}, 300);
$(this).siblings('.pull_down_content').children('.inner').css({'display': 'none'}, 300);
}
}
}
});
});
我不确定这是否与此有关?
if(!$('.timelineTile').hasClass("clicked")){
$(this).children('.pull_down_content').height(0);
}
尝试更换
$('.timelineTile').not(this).removeClass('clicked').find('.pull_down_content').height(0);
用这个
$('.timelineTile').not(this).removeClass('clicked').find('.pull_down_content').height(0).end().find('.item').data('clicked',false);
当元素不存在时(例如,如果它们将由脚本动态添加),您可能会尝试附加事件。
使用更现代的案例"on"而不是"click"等。
$(wrapper).on('click', 'element', function() { ... });
Smth样:
...
<div class="wrapper">
<span class="link">Click me</span>
</div>
...
$('.wrapper').on('click', '.link', function() { ... });
此变体为所有元素添加事件,即使它们是动态添加的。
相关文章:
- 单击仅在第二次单击后有效
- 单击图像时不会出现圆圈,但对其他人有效
- 文档就绪按钮.单击仅在控制台中有效
- jQuery onClick 事件有效,除非单击文本
- 显示隐藏的容器元素 - 悬停仅在我单击容器后有效
- 按钮单击Javascript函数失败,但函数有效
- 如果第一次单击有效,则第二次单击元素
- 使可折叠侧边栏记住单击的选项的有效方法
- jQuery 单击仅在双击时才有效
- 奇怪的是,在第二次单击切换隐藏/显示按钮而不是第一次单击后,有两行有效
- 当我单击删除按钮时查询未运行,即使 .success 说它有效
- 我的幻灯片网站仅在单击按钮两次以运行 JS 时才有效
- ng有效,当值通过<按钮>ng单击
- 在文本框中键入内容后,单击按钮将不再有效
- 为什么悬停和单击不总是有效
- 启用右键单击的更有效方法
- 复选框单击有效,取消选中复选框不工作Javascript
- 状态栏中的屏蔽链接仅在第一次单击之前有效
- 不能使用CasperJS调度单击有效的选择器
- on('提交'在单击<按钮>时有效,但在输入字段上按enter键时无效