admin管理员组文章数量:1414628
I am trying to make a select-unselect image by change border color on click by this code
var $box=null;
$('img')
.click(function() {
if ($box == null) {
$box = $(this);
$box.css("border","5px solid green");
} else {
$box.css("border","5px solid white");
$box = null;
}
}
);
The code is working fine except when I try to select-unselect and select same image. I want to select the other image by one click.
I tried to check if ($box == $(this))
but it does not work.
I am trying to make a select-unselect image by change border color on click by this code
var $box=null;
$('img')
.click(function() {
if ($box == null) {
$box = $(this);
$box.css("border","5px solid green");
} else {
$box.css("border","5px solid white");
$box = null;
}
}
);
The code is working fine except when I try to select-unselect and select same image. I want to select the other image by one click.
I tried to check if ($box == $(this))
but it does not work.
- Is it important to store the selected state (for use in other parts of your code) or do you just want to toggle the style of the border? – rink.attendant.6 Commented Aug 21, 2013 at 13:22
- Yes, I need to store null state(no one selected) for other control – LE SANG Commented Aug 21, 2013 at 13:23
- Ok, and are you saying that in a set of images, only 1 can be selected? – rink.attendant.6 Commented Aug 21, 2013 at 13:25
- Yes, only-one or No-one(select then de-selected) selected – LE SANG Commented Aug 21, 2013 at 13:26
5 Answers
Reset to default 4Use a class instead, and toggle the class when needed. This solution acts like a radio button (only one image with a border at a time), but allows you to deselect the active image as well:
http://jsfiddle/6cGVz/
$('img').click(function() {
var $this = $(this);
if ($this.hasClass('active')) {
$this.removeClass('active');
} else {
$('.active').removeClass('active');
$this.addClass('active');
}
});
Explanation
Check if $box
is the clicked element or not. If it is, just hide its border if it has one. Otherwise, put the border on the clicked element!
Solution (Live Demo)
JavaScript/JQuery
var $box=null;
$('img')
.click(function() {
if ($box == null) {
$box = $(this);
$box.css("border","5px solid green");
} else {
$box.css("border","5px solid white");
if($box != $(this))
{
$box = $(this);
$box.css("border","5px solid green");
}
else
$box = null;
}
}
);
For the purposes of your question, I will put all of the images in a container:
<div id='setOfImages'>
<img ... >
<img ... >
<img ... >
<img ... >
</div>
Toggle a class.
$('#setOfImages > img').click(function() {
'use strict';
if($(this).hasClass('selected')) {
// Deselect currently selected image
$(this).removeClass('selected');
} else {
// Deselect others and select this one
$('#setOfImages > img').removeClass('selected');
$(this).addClass('selected');
}
});
And in your CSS:
#setOfImages > img {
border: 5px solid #fff;
}
#setOfImages > img.selected {
border: 5px solid green;
}
See jsFiddle demo.
Update - only one image can be selected
toggleClass of jQuery method make it so easy -
Using Js -
$('img').click(function() {
if ($(this).hasClass("selected")) {
$("img.selected").removeClass("selected");
} else {
$("img.selected").removeClass("selected");
$(this).addClass("selected");
}
});
with css -
.selected{
border:5px solid green;
}
Demo
You can add a data attribute to the image itself, instead of relying on something external.
$('img')
.click(function() {
var img = $(this);
if (! img.data('box')) {
img.css("border","5px solid green");
img.data('box', true);
} else {
img.css("border","5px solid white");
img.data('box', false);
}
}
);
A working example: http://codepen.io/paulroub/pen/qbztj
本文标签: javascriptHow to switch image on click by change border in jQueryStack Overflow
版权声明:本文标题:javascript - How to switch image on click by change border in jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745167367a2645762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论