我正在尝试使用HTML,CSS,JS实现以下目标。
我有两个复选框和4张图片:
<input id="chk-box1" type="checkbox"> checkbox 1 </input>
<input id="chk-box2" type="checkbox" > checkbox 2 </input>
<img alt="" id="1" src="1.jpg" />
<img alt="" id="2" src="2.jpg" />
<img alt="" id="3" src="3.jpg" />
<img alt="" id="4" src="4.jpg" />
答案 0 :(得分:1)
使用一些jquery来处理显示图像
if ($("#chk-box1").is(":checked"))
{
if ($("#chk-box2").is(":checked"))
{
$("#1").hide()
$("#2").hide()
$("#3").hide()
$("#4").show()
}
else
{
$("#1").hide()
$("#2").show()
$("#3").hide()
$("#4").hide()
}
}
else
{
if ($("#chk-box2").is(":checked"))
{
$("#1").hide()
$("#2").hide()
$("#3").show()
$("#4").hide()
}
else
{
$("#1").show()
$("#2").hide()
$("#3").hide()
$("#4").hide()
}
}
答案 1 :(得分:1)
这应该会有所帮助: DEMO
<强> JS 强>
var shown = 1;
function toggleImages() {
if (document.getElementById('chk-box1').checked) {
if (document.getElementById('chk-box2').checked) {
showImage(4);
} else {
showImage(2);
}
} else if (document.getElementById('chk-box2').checked) {
showImage(3);
} else {
showImage(1);
}
}
function showImage(n) {
document.getElementById(shown).style.display = 'none';
document.getElementById(n).style.display = 'block';
shown = n;
}
<强> HTML 强>
<input id="chk-box1" type="checkbox" onchange="toggleImages();">checkbox 1</input>
<input id="chk-box2" type="checkbox" onchange="toggleImages();">checkbox 2</input>
<br/>
<img alt="1" id="1" src="1.jpg" title="1"/>
<img alt="2" id="2" src="2.jpg" title="2" style="display:none;" />
<img alt="3" id="3" src="3.jpg" title="3" style="display:none;" />
<img alt="4" id="4" src="4.jpg" title="4" style="display:none;" />
答案 2 :(得分:1)
目前,答案是硬编码使用元素的ID。
我构建了一种不同的方法,其中一个使用js-
类来绑定javascript,而数据属性则定义应该在何时显示哪个图像。如果应该添加多个复选框,并且应该更改图像的显示方式,这会使代码硬编码更少,并且可以扩展。
(function($, document) {
"use strict";
var $document = $(document);
var CheckboxHandler = {
$checkboxes: null,
$images: null,
bind: function() {
var self = this;
this.$checkboxes.each(function() {
var $checkbox = $(this);
$checkbox.on('change', function() {
self.triggerImageToShow();
});
});
},
triggerImageToShow: function() {
var showImageFor = [];
this.$checkboxes.each(function() {
var $checkbox = $(this);
if ($checkbox.is(':checked')) {
showImageFor.push(parseInt($checkbox.attr('data-checkbox')));
}
});
this.$images.each(function() {
var $image = $(this);
$image.trigger('show-image-for-checkboxes', [showImageFor]);
})
},
init: function($checkboxes, $images) {
this.$checkboxes = $checkboxes;
this.$images = $images;
this.bind();
}
};
var Image = {
showForCheckboxes: [],
bind: function() {
var self = this;
this.$image.on('show-image-for-checkboxes', function(event, showForCheckboxes) {
var isArrayEqual = JSON.stringify(showForCheckboxes) == JSON.stringify(self.showForCheckboxes)
if (isArrayEqual) {
self.$image.show();
} else {
self.$image.hide();
}
});
},
init: function($image) {
this.$image = $image;
this.showForCheckboxes = $image.data('show-for-checkboxes');
this.bind();
}
};
$document.ready(function() {
var $images = $('.js-toggle-image');
var $checkboxes = $('.js-toggler');
var checkboxHandler = Object.create(CheckboxHandler);
checkboxHandler.init($checkboxes, $images);
var startToShowImageFor = [];
$images.each(function() {
var image = Object.create(Image);
image.init($(this));
image.$image.trigger('show-image-for-checkboxes', [startToShowImageFor]);
});
});
}(jQuery, document));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<label>
<input class="js-toggler" data-checkbox="1" type="checkbox" />checkbox 1
</label>
<label>
<input class="js-toggler" data-checkbox="2" type="checkbox" />checkbox 2
</label>
</div>
<div>
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[]" src="http://dummyimage.com/600x400/000/fff&text=First+image" />
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[1]" src="http://dummyimage.com/600x400/000/fff&text=Second+image" />
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[2]" src="http://dummyimage.com/600x400/000/fff&text=Third+image" />
<img alt="" class="js-toggle-image" data-show-for-checkboxes="[1,2]" src="http://dummyimage.com/600x400/000/fff&text=Fourth+image" />
</div>