移除添加类别为'selected'的项目上的点击事件
Remove click event on item with added class of 'selected'
我是新手,也是JavaScript和JQuery的新手。
我一直在写一个小脚本,让我的页面从Youtube上切换视频的一部分。
我遇到的问题是,当缩略图添加了"选定"类时,我想禁用点击功能以停止视频重新加载-是否有一个简单的方法?
我试图把另一点JQuery在找到项目与'selected'类和返回false,但我知道这是错误的,因为这将停止默认的浏览器行为,而不是点击事件。
我也非常感谢你对代码建议的解释,以及改进/简化我的JQuery代码的建议。
提前感谢!
HTML:<iframe id="vid" width="980" height="551" src="http://www.youtube.com/embed/hziG9Nr6KHU?&rel=0&controls=4&wmode=transparent&modestbranding=1&rel=0&showinfo=1" frameborder="0" allowfullscreen></iframe>
<ul id="videos">
<li id="one">
<a class="selected" href="http://www.youtube.com/embed/hziG9Nr6KHU?&rel=0&controls=2&wmode=transparent&modestbranding=1&rel=0&showinfo=1&autoplay=1 ">
<img src="http://placehold.it/320x180" />
<span class="AGreg">Vid1</span>
</a>
</li>
<li id="two" class="left">
<a href="http://www.youtube.com/embed/V1bFr2SWP1I?&rel=0&controls=2&wmode=transparent&modestbranding=1&rel=0&showinfo=1&autoplay=1 ">
<img src="http://placehold.it/320x180" />
<span class="AGreg">Vid12</span>
</a>
</li>
<li id="three" class="left">
<a href="http://www.youtube.com/embed/XLgYAHHkPFs?&rel=0&controls=2&wmode=transparent&modestbranding=1&rel=0&showinfo=1&autoplay=1 ">
<img src="http://placehold.it/320x180" />
<span class="AGreg">Vid3</span>
</a></li>
</ul>
CSS: iframe#vid{width:980px;height:551px;}
.selected img{
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */}
#videos .cursorD a{cursor:default!important}
#videos a:hover img{
opacity: 0.4;
filter: alpha(opacity=40); /* For IE8 and earlier */}
ul#videos{margin:20px 0;padding:0;}
ul#videos img{}
ul#videos li{float:left;width:320px;}
ul#videos li.left{margin:0 0 0 10px}
JQUERY: $(document).ready(function(){
$("ul#videos li a").click(function(){
$("iframe").attr("src", $(this).attr("href"));
$(this).closest('ul#videos').find('.selected').removeClass('selected');
$(this).parent().addClass('selected cursorD');
return false;
})
});
CODEPEN : http://codepen.io/PixelsPencil/pen/uljzy
您可以随心所欲地删除点击视频上的事件处理程序,但这需要一些代码。一个更简单和(i.m.h.o.更优雅)的解决方案是调整你的选择器/事件处理程序连接,使它只在没有类"selected"的元素上执行:
$(document).ready(function(){
$("ul#videos li a:not('.selected')").click(function(){
$("iframe").attr("src", $(this).attr("href"));
$(this).closest('ul#videos').find('.selected').removeClass('selected');
$(this).parent().addClass('selected cursorD');
return false;
})
});
我必须承认,我没有测试它,我不能100%确定:xyz()
类型选择器的评估优先级。要更确定正在发生什么,您可以将单个选择器更改为选择器链:
$("ul#videos").find("li").find("a:not('.selected')").click( ... );
或者更好的是,将事件处理程序与父对象挂钩,但只在不同的元素上触发(也通过选择器表示):
$("ul#videos").on("click", "li a:not('.selected')", function() { ... });
这具有附加的魅力,处理程序将自动工作于在连接事件处理程序后添加的li
。
这似乎有效-向几个人寻求帮助,一位同事想出了这个仍然不确定为什么/如何工作,但它确实有效。
var VideoLoader = {
registerClickHandlers: function() {
$("ul#videos li a").click(VideoLoader.showSelectedVideo);
},
showSelectedVideo: function() {
if(!VideoLoader.checkVideoIsAlreadyPlaying(this)) {
$("iframe").attr("src", $(this).attr("href"));
$(this).closest('ul#videos').find('.selected').removeClass('selected cursorD');
$(this).parent().addClass('selected cursorD');
}
return false;
},
checkVideoIsAlreadyPlaying: function(clickedObject) {
return $(clickedObject).parent().hasClass("selected");
}
};
$(document).ready(VideoLoader.registerClickHandlers);
更新了我的Codepen: http://codepen.io/PixelsPencil/pen/uljzy
感谢您的回复!我有一个剧本,在jsfiddle上放了一些东西,但现在它比以前更坏了,哈哈。当我点击一些没有选择的类所有拇指改变类,当我点击一个选定的类视频加载在父,而不是切换在iframe的attr -任何想法我哪里错了??jsfiddle.net/wvPnA
相关文章:
- 将单击事件添加到附加的项目中
- 事件为所有项目而不仅仅是当前项目激发
- 在控制器中创建StandardListItem并在赢得的项目上按下事件'不要开火
- 语义 UI 手风琴获取打开事件中打开的项目的索引
- 在复选框取消选中事件中从HTML下拉列表中删除项目
- 绑定到项目查看更改事件
- 事件列表器不适用于追加的项目
- 有没有办法控制台.log所有事件发生在骨干木偶项目视图中
- 点击事件:选择要按 ID 点击的项目
- 如何只调用一次事件处理程序,而不是每次单击项目时调用事件处理程序
- 时间轴.js如何将鼠标悬停事件添加到可视化项目框框
- 在 AngularJS 中单击事件时将项目从一个数组复制到另一个数组
- 如何在 ReactJS 中使用单击事件从列表中删除项目
- window.onload 事件不会触发或“如何正确初始化 js 项目?”
- 在我的 javascript 项目中获取不正确的值时,将函数作为事件处理程序的回调传递
- 选择项目时,kendoListView 更改事件会触发两次
- 用于使用 Tab 键选择项目的事件
- on单击li项目上的事件,以更改文本字段
- simpleCart(js) 'beforeAdd' 事件用于多个项目
- iPhone上的Three.js项目-事件问题(选择和拖动对象)