在队列jQuery中添加添加/删除类点击图片
Add add/remove class click picture in queue jQuery
我有那些图片
<img src=img1.jpg class=pic />
<img src=img2.jpg class=pic />
<img src=img3.jpg class=pic />
<img src=img4.jpg class=pic />
<img src=img5.jpg class=pic />
<img src=img6.jpg class=pic />
.ShowBorderRed{border:3px solid red;}
我想在单击其中一个图片后添加类.ShowBorderRed
,在单击另一张图片并将该类添加到此新图像后删除该类。JQuery
使用以下内容:
$(document).ready(function(){
var $img = $('.pic');
$img.click(function(event){
$img.removeClass('ShowBorderRed');
$(this).addClass('ShowBorderRed');
});
});
查看代码中的内联注释:
// bind click event on all the images having pic class
$('img.pic').on('click', function() {
$(this).addClass('ShowBorderRed') // Add class to the clicked image
.siblings().removeClass('ShowBorderRed'); // Remove class from other sibling images
});
演示
或
如果图像不是siblings
:
var $images = $('img.pic');
$images.on('click', function() {
$images.removeClass('ShowBorderRed'); // Remove class from all other images
$(this).addClass('ShowBorderRed'); // Add class to the clicked image
});
DEMO
Use the following code:
$(document).ready(function(){
var $img = $('.pic');
$img.click(function(event){
$img.removeClass('ShowBorderRed');
$(this).addClass('ShowBorderRed');
});
});
refer the below mentioned link.
http://jsfiddle.net/2QyY3/199/
相关文章:
- 正在添加'X'按钮,在文本字段旁边使用javascript
- 如何在映射数组中添加换行符
- 正在将数据主题添加到所有项目
- ZeroClipboard-在复制之前添加到值
- 我可以在json对象中添加一个函数吗
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何添加浮动和非浮动,其他
- 添加文字和评论功能更新Div
- 如果使用 lodash 将属性存在于另一个对象中,则向对象添加属性
- 将JS函数添加到并行队列中
- jQuery向动画队列添加函数
- Javascript 轮播 - 添加队列时出现问题
- 在Javascript中将某个内容添加到队列或数组时激发事件
- 如何在队列中自动生成的刻度中添加新的刻度
- 将blob添加到dropzone.js队列而不是file
- 在队列jQuery中添加添加/删除类点击图片
- 如何遍历队列,随时添加和删除元素
- 如何向队列中动态添加函数
- 添加时从队列中删除文件