这是我的 HTML :
<div id="show">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR51FTd5w9iwfz_PLnUTUIbYBB0bCX6d3ue1ZSx3SJObNLGECEm"
>
</div>
<div id="thumbnails">
<div id="thumbnail1">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPIzMxDNjQ_x4iUZ6WLo9R32QKCreu8PQGxcRHWmUw4hNcYmiR"
width="100" height="100">
</div>
<div id="thumbnail2">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR51FTd5w9iwfz_PLnUTUIbYBB0bCX6d3ue1ZSx3SJObNLGECEm"
width="100" height="100">
</div>
<div id="thumbnail3">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTsplNlIS3zRyy89UxlT5Nwu_Bn7m6w7iqMYXPF9q9MpLOG17XR"
width="100" height="100">
</div>
我的 CSS :
#thumbnails div {
float:left;
border:1px solid;
}
#thumbnails div:hover {
color:yellow;
}
所以,我只想在点击时用其下面的任何缩略图更改div #show
。我已经尝试使用jQuery .attr('src', 'url');
,但效果不佳。
小提琴:http://jsfiddle.net/PemHv/
谢谢
答案 0 :(得分:8)
用户.on()
将点击事件分配给dom和.attr()
以获取DOM
的属性值。
$('#thumbnails img').on('click',function(){
var src = $(this).attr('src');
$('#show img').attr('src',src);
});
<强>解释强>
在上面的代码中,我将click
事件分配给身份div
的{{1}}内的图片,现在首先获取thumbnails
的{{1}}并设置它进入src attribute
变量。
然后在下一行中替换clicked image
内的src
图片。
答案 1 :(得分:1)
演示:http://jsfiddle.net/PemHv/3/
jQuery的:
$('img',".thumbnail").click(function(){
var src = $(this).attr('src');
$('img',$('#show')).attr('src',src);
});
HTML(你应该使用类):
<div id ="show">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR51FTd5w9iwfz_PLnUTUIbYBB0bCX6d3ue1ZSx3SJObNLGECEm" width="200" height="200">
</div>
<div id="thumbnails">
<div class="thumbnail">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPIzMxDNjQ_x4iUZ6WLo9R32QKCreu8PQGxcRHWmUw4hNcYmiR" width="100" height="100">
</div>
<div class="thumbnail">
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR51FTd5w9iwfz_PLnUTUIbYBB0bCX6d3ue1ZSx3SJObNLGECEm" width="100" height="100">
</div>
</div>
答案 2 :(得分:1)
$('#thumbnails div').click(function(){
var path = $(this).find('img').attr("src");
//console.log(path)
$('#show img').attr("src", path );
});
答案 3 :(得分:0)
$('#show')
.children()
.on('click', function(event) {
$(event.target).attr('src', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTPIzMxDNjQ_x4iUZ6WLo9R32QKCreu8PQGxcRHWmUw4hNcYmiR');
});