1
基本上我希望用户能够点击和更改背景,并为那里有多个背景,为特定的div。更改div背景图像,JS在铬但不是IE或Firefox的作品?
这在Google Chrome中完美工作,但不在IE或Firefox中。
HTML:
<div id="palette">
<div class="lightblue"></div>
<div class="gold"></div>
<div class="aqua"></div>
</div>
<div id="primary">
</div>
CSS:
#palette {
border: 2px solid white;
width: 25px;
}
#palette div {
height: 25px;
width: 25px;
}
.lightblue {
background: lightblue url(http://www.textureking.com/content/img/stock/big/DSC_4279.JPG);
}
.gold {
background: gold url(http://www.textureking.com/content/img/stock/big/DSC_4287.JPG);
}
.aqua {
background: aqua url(http://www.textureking.com/content/img/stock/big/DSC_4274.JPG);
}
JS:
$(document).ready(function() {
// attach onclick event to your palette colors
$('#palette div').on('click', function() {
// get background of selected palette color
var bg = $(this).css('background');
// change the background of the body
$('#primary').css({ 'background': bg });
});
});
它没有显示任何错误和其他JavaScript运行,所以我不确定问题是什么。
如果它是很容易解决,请留下一个答案,如果没有,我会这样试试:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_backgroundimage
嗯或许可以创建一个cookie来记住他们选择的背景? – amuletts 2014-10-05 23:09:04