jQuery - 如何在点击的元素上替换img src? [英] jQuery - How to replace img src on clicked element?
本文介绍了jQuery - 如何在点击的元素上替换img src?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML:
<div id="panel">
<a href="path_to_image1.jpg" rel="shadowbox[page]">
<img src="scripts/timthumb.php?src=path_to_image1.jpg&w=200&h=20" alt="Image name1" /></a>
</div>
<div id="thumbs">
<a href="path_to_image1.jpg" rel="shadowbox[page]"><img src="scripts/timthumb.php?src=path_to_image1.jpg&w=30&h=30" alt="Image name1" /></a>
<a href="path_to_image2.jpg" rel="shadowbox[page]"><img src="scripts/timthumb.php?src=path_to_image2.jpg&w=30&h=30" alt="Image name2" /></a>
<a href="path_to_image3.jpg" rel="shadowbox[page]"><img src="scripts/timthumb.php?src=path_to_image3.jpg&w=30&h=30" alt="Image name3" /></a>
</div>
JS:
$('#thumbs a').click(function(evt) {
evt.preventDefault();
var clickedElement = $(this).clone();
$('#panel').empty().append(clickedElement);
});
所以这很有用,我只想更改w = 30& h = 30的图像src点击锚定到w = 200& h = 200,然后附加到#panel。
So this works and i want just to change w=30&h=30 of image src on clicked anchor to w=200&h=200 and then to be appended to the #panel.
推荐答案
$('#thumbs a').click(function(evt) {
var clickedElement = $(this).clone().find('img').attr('src', function(i, src){
return src.replace('w=30&h=30', 'w=200&h=200')
}).end()
$('#panel').html(clickedElement);
});
这篇关于jQuery - 如何在点击的元素上替换img src?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文