我正在尝试使此代码正常工作。如果 URL 中有#leaf,那么标签会更新为显示 image2.png。我不确定我做错了什么。
<script type="text/javascript">
$("#image").show("fast") {
// Which anchor is being used?
switch(window.location.hash) {
case "#leaf":
window.location.href = image1.png
break;
case "#carrot":
window.location.href = image2.png
break;
}
});
</script>
<a href="#leaf">Leaf</a>
<a href="#carrot">Carrot</a>
<img id="image" />
PS:我愿意接受更好的方法来实现这一点。刚刚尝试了以下但仍然无法正常工作。
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.min.js"></script>
<script type="text/javascript">
$("#image").show("fast", function() {
// Which anchor is being used?
switch(window.location.hash) {
case "#leaf":
$(this).attr('src', 'image1.png');
break;
case "#carrot":
$(this).attr('src', 'image2.png');
break;
}
});
</script>
</head>
<body>
<a href="#leaf">Leaf</a>
<a href="#carrot">Carrot</a>
<img id="image" />
</body>
</html>