我有一个简单的html页面,可根据下拉菜单中的选择显示图像。
<head>
<script src="jquery.min.js"></script>
</head>
<div id="mymenu">
<select id="men" onchange="$('#imageToSwap').attr('src', this.options[this.selectedIndex].value);chngSel()">
<option value="product_a.png" selected>Product A</option>
<option value="product_b.png" selected>Product B</option>
<option value="product_c.png" selected>Product C</option>
</select>
</div>
<img id="imageToSwap" class="profile" src="all_products.png">
</head>
</html>
根据所选选项显示图像。
选择“产品B”选项后,将显示product_b.png,一旦单击product_b.png,是否可以将超链接更改为product_b.html页?显示的图片是否具有变量href?
答案 0 :(得分:0)
您的HTML
<head>
<script src="jquery.min.js"></script>
</head>
<body>
<div id="mymenu">
<select id="men">
<option data-url="/productA" value="product_a.png">Product A</option>
<option data-url="/productB" value="product_b.png">Product B</option>
<option data-url="/productC" value="product_c.png">Product C</option>
</select>
</div>
<img id="imageToSwap" class="profile" src="all_products.png">
</body>
</head>
</html>
jQuery
$(function() {
var $imageToSwap = $('#imageToSwap');
// change the source of the image
$('#men').on('change', function(e) {
var $this = $(this),
src = $this.val(),
url = $this.data('url');
$imageToSwap.attr({ src }).data({ url });
});
// click event for the image
$imageToSwap.on('click', function() {
var $this = $(this),
url = $this.data('url');
location.href = url;
});
});
答案 1 :(得分:-1)
不确定,但是不能一次拥有3个“选择的”选项。 选定用作“默认选择”。尝试只使用一次。