不确定之前是否曾询问过此事。新来的和JS。这是我的阿喀琉斯之踵。
快速浏览:如您所见,我已将图像放置在数组中以便于操作。然后我告诉浏览器索引中的#assignments == image 0,字体应该是绿色,与else if和else语句相同。
问题是浏览器会跳过除else之外的所有语句。我相信浏览器正在将它们评估为虚假并继续前进,直到它到达最后一个它看起来是真的并执行它。这种思路让我相信语句中有一个错误,很可能是在参数中,尽管我没有看到它。
启动JavaScript
<script type="text/javascript">
$(function fontFunction() {
var links = $('#splash');
var body = $('#assignments');
var backgrounds = [
'url(css/images/violin.jpg)',
'url(css/images/chateau.jpg)', //I own this photo. Taken at Château de Vaux-le-Vicomte, 77950 Maincy, France.
'url(css/images/museum.jpg)', //I own this photo. Taken at Peabody Essex Museum, Salem Massachusetts
'url(css/images/creating.jpg)'
];
if (body.css('background-image') == backgrounds[0]) {
links.css("color", "green");
alert("hello!");
} else if (body.css('background-image') == backgrounds[1]) {
links.css("color", "purple");
} else if (body.css('background-image') == backgrounds[2]) {
links.css("color", " red");
} else {
links.css("color", "orange");
}
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container" id="assignments">
<p id="splash"><b>always create.</b>
</p>
</div
答案 0 :(得分:3)