JavaScript在单击时交换/更改图像
我想更改下面用于将图像更改为链接/图像的按钮。任何帮助都将不胜感激!谢谢JavaScript在单击时交换/更改图像,javascript,image,Javascript,Image,我想更改下面用于将图像更改为链接/图像的按钮。任何帮助都将不胜感激!谢谢 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <style> </st
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type='text/javascript'>
onload = function() {
var buttonData = [
{src:'fareast.gif', href:'http://google.com'},
{src:'middlearth.jpg', href:'http://yahoo.com'},
{src:'europe.jpg', href:'http://chappo.cc'}
];
var $im1 = $("[name='im1']").click(function(){ window.open($(this).attr('href')); });
$(".imgSwap").each(function(i) {
var $b = $(this), d = buttonData[i] || buttonData[buttonData.length-1];
$b.click(function() { $im1.attr({src:d.src,title:$(this).html(),href:d.href}); });
if(i==0) { $b.click(); }
});
};
</script>
</head>
<body>
<img name="im1" height="300" width="300" /><br/>
<button class="imgSwap">Far East</button>
<button class="imgSwap">Middle Earth</button>
<button class="imgSwap">Europe</button>
</body>
</html>
onload=函数(){
变量buttonData=[
{src:'fareast.gif',href:'http://google.com'},
{src:'middlerth.jpg',href:'http://yahoo.com'},
{src:'europe.jpg',href:'http://chappo.cc'}
];
var$im1=$(“[name='im1']”)。单击(函数(){window.open($(this.attr('href'));});
$(“.imgSwap”)。每个(函数(i){
变量$b=$(此),d=ButtonDa[i]| | ButtonDa[ButtonDa.length-1];
$b.click(函数(){$im1.attr({src:d.src,title:$(this.html(),href:d.href});});
如果(i==0){$b.click();}
});
};
远东
中土
欧洲
请帮忙 首先,HTML:
<a href="#" class="imgSwap"><img src="path/to/image/far_east_button.gif" class="buttonImage" /></a>
<a href="#" class="imgSwap"><img src="path/to/image/middle_earth.gif" class="buttonImage" /></a>
<a href="#" class="imgSwap"><img src="path/to/image/europe.gif" class="buttonImage" /></a>
然后,单击处理程序:
$(".imgSwap").each(function(i) {
var $b = $(this), d = buttonData[i] || buttonData[buttonData.length-1];
$b.click(function() {
$im1.attr({src:d.src, title:d.title, href:d.href});//note change to composition of title
return false;//Added to suppresses default hyperlink action of <a> links.
}).attr('title', d.title);
if(i==0) { $b.click(); }
});
$(“.imgSwap”)。每个(函数(i){
变量$b=$(此),d=ButtonDa[i]| | ButtonDa[ButtonDa.length-1];
$b.单击(函数(){
$im1.attr({src:d.src,title:d.title,href:d.href});//注意对标题组成的更改
return false;//添加到抑制链接的默认超链接操作。
}).attr(“头衔”,d.头衔);
如果(i==0){$b.click();}
});
希望这能解决问题。首先,HTML:
<a href="#" class="imgSwap"><img src="path/to/image/far_east_button.gif" class="buttonImage" /></a>
<a href="#" class="imgSwap"><img src="path/to/image/middle_earth.gif" class="buttonImage" /></a>
<a href="#" class="imgSwap"><img src="path/to/image/europe.gif" class="buttonImage" /></a>
然后,单击处理程序:
$(".imgSwap").each(function(i) {
var $b = $(this), d = buttonData[i] || buttonData[buttonData.length-1];
$b.click(function() {
$im1.attr({src:d.src, title:d.title, href:d.href});//note change to composition of title
return false;//Added to suppresses default hyperlink action of <a> links.
}).attr('title', d.title);
if(i==0) { $b.click(); }
});
$(“.imgSwap”)。每个(函数(i){
变量$b=$(此),d=ButtonDa[i]| | ButtonDa[ButtonDa.length-1];
$b.单击(函数(){
$im1.attr({src:d.src,title:d.title,href:d.href});//注意对标题组成的更改
return false;//添加到抑制链接的默认超链接操作。
}).attr(“头衔”,d.头衔);
如果(i==0){$b.click();}
});
希望这能解决问题。这样做
<script>
function changeImg(val)
{
if(val == 1)
{
document.getElementById('im1').src = "fareast.gif";
}
if(val == 2)
{ document.getElementById('im1').src = "middlearth.jpg";}
if(val == 3)
{ document.getElementById('im1').src = "europe.jpg";}
}
</script>
<img name="im1" id='im1' height="300" width="300" /><br/>
<button class="imgSwap" onclick="changeImg('1');">Far East</button>
<button class="imgSwap" onclick="changeImg('2');">Middle Earth</button>
<button class="imgSwap" onclick="{changeImg('3');">Europe</button>
功能更改IMG(val)
{
如果(val==1)
{
document.getElementById('im1').src=“fareast.gif”;
}
如果(val==2)
{document.getElementById('im1').src=“middlerth.jpg”;}
如果(val==3)
{document.getElementById('im1').src=“europe.jpg”;}
}
远东
中土
欧洲
执行此操作
<script>
function changeImg(val)
{
if(val == 1)
{
document.getElementById('im1').src = "fareast.gif";
}
if(val == 2)
{ document.getElementById('im1').src = "middlearth.jpg";}
if(val == 3)
{ document.getElementById('im1').src = "europe.jpg";}
}
</script>
<img name="im1" id='im1' height="300" width="300" /><br/>
<button class="imgSwap" onclick="changeImg('1');">Far East</button>
<button class="imgSwap" onclick="changeImg('2');">Middle Earth</button>
<button class="imgSwap" onclick="{changeImg('3');">Europe</button>
功能更改IMG(val)
{
如果(val==1)
{
document.getElementById('im1').src=“fareast.gif”;
}
如果(val==2)
{document.getElementById('im1').src=“middlerth.jpg”;}
如果(val==3)
{document.getElementById('im1').src=“europe.jpg”;}
}
远东
中土
欧洲
如果我理解正确,只需更改你的代码即可工作:如果我理解正确,只需更改你的代码即可工作:你的javascript能力让我嫉妒你的javascript能力让我嫉妒