admin管理员组文章数量:1357232
i have a image with checkbox, i used this code for Checking a checkbox by clicking an image...
<label for="img1"><img class="img" src="myimage.jpg" /></label>
<input type="checkbox" class="chk " checked="checked" id="img1" name="img1" value="0" />
its working fine, my question is, i want when checking check box above image opacity will be 0.5
or display a extra div
over this image and when Un check above image opacity return to 1.0
or remove that div if we use div, any idea.???
thanks
i have a image with checkbox, i used this code for Checking a checkbox by clicking an image...
<label for="img1"><img class="img" src="myimage.jpg" /></label>
<input type="checkbox" class="chk " checked="checked" id="img1" name="img1" value="0" />
its working fine, my question is, i want when checking check box above image opacity will be 0.5
or display a extra div
over this image and when Un check above image opacity return to 1.0
or remove that div if we use div, any idea.???
thanks
Share Improve this question asked Apr 1, 2013 at 8:38 mansmans 1,0874 gold badges27 silver badges45 bronze badges 1- This has been answered here: stackoverflow./questions/5275857/… – aaaaaa Commented Apr 1, 2013 at 8:43
3 Answers
Reset to default 6You can acplish this using some advanced CSS selectors provided that the <label>
is the checkbox. The ~
selector is called general sibling selector and will match all sibling after the element.
jsFiddle
CSS
input:checked ~ label {
opacity: 0.5;
}
HTML
<input type="checkbox" class="chk " checked="checked" id="img1" name="img1" value="0" />
<label for="img1">
<img class="img" src="https://www.google..au/images/srpr/logo4w.png" />
</label>
Support
Support for :checked
is only present for IE9 and above, if you need support for IE8 then you can you use a .checked
class as use it in addition to the CSS3 :checked
selector.
JS
$('#img1').click(function () {
$(this).toggleClass('checked');
});
CSS
input:checked ~ label,
input.checked ~ label {
opacity: 0.5;
}
For support < IE8 then you should use a more general JavaScript solution.
$('#img1').click(function () {
$('label[for=' + this.id + ']').toggleClass('checked');
});
label.checked {
opacity: 0.5;
}
$('.img').click(function() {
if ($('#img1').is(':checked'))
$(this).css('opacity', 0.5);
else
$(this).css('opacity', 1);
});
$('.img').click(function() {
$(this).css('opacity', $('#img1').is(':checked')?0.5:1.0);
});
本文标签: javascriptChecking a checkbox by clicking an image with changing image opacityStack Overflow
版权声明:本文标题:javascript - Checking a checkbox by clicking an image with changing image opacity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744072521a2586147.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论