简体   繁体   English

如何使用 jQuery 更改单击时的图像源?

[英]How can I change image source on click with jQuery?

I 'm currently building a full background image layout and I want to change the image based on which page the user is visiting.我目前正在构建一个完整的背景图像布局,我想根据用户正在访问的页面更改图像。 To get to the point: I need to change a images attribute when the user clickes on a link.直截了当:我需要在用户单击链接时更改图像属性。 This is how far I got:这是我走了多远:

$(function() {
  $('.menulink').click(function(){
    $("#bg").attr('src',"img/picture1.jpg");
  });
});

    <a href="" title="Switch" class="menulink">switch me</a>
    <img src="img/picture2.jpg" id="bg" />

Thank you, probably easy stuff, but over my head!谢谢,可能是简单的事情,但我头疼!

It switches back because by default, when you click a link, it follows the link and loads the page.它切换回去是因为默认情况下,当您单击链接时,它会跟随链接并加载页面。 In your case, you don't want that.就您而言,您不希望那样。 You can prevent it either by doing e.preventDefault();您可以通过执行 e.preventDefault(); 来阻止它。 (like Neal mentioned) or by returning false: (就像提到的 Neal 一样)或者通过返回 false:

$(function() {
 $('.menulink').click(function(){
   $("#bg").attr('src',"img/picture1.jpg");
   return false;
 });
});

Interesting question on the differences between prevent default and return false.关于防止默认和返回 false 之间差异的有趣问题

In this case, return false will work just fine because the event doesn't need to be propagated.在这种情况下,return false 会很好地工作,因为不需要传播事件。

You need to use preventDefault() to make it so the link does not go through when u click on it:您需要使用preventDefault()来制作它,以便在您单击它时链接不会通过:

fiddle: http://jsfiddle.net/maniator/Sevdm/小提琴:http: //jsfiddle.net/maniator/Sevdm/

$(function() {
 $('.menulink').click(function(e){
     e.preventDefault();
   $("#bg").attr('src',"img/picture1.jpg");
 });
});

You can use jQuery's attr() function, like $("#id").attr('src',"source") .您可以使用 jQuery 的attr()函数,例如$("#id").attr('src',"source")

 $('div#imageContainer').click(function () {
      $('div#imageContainerimg').attr('src', 'YOUR NEW IMAGE URL HERE'); 
});

You should consider using a button for this.您应该考虑为此使用一个按钮。 Links generally should be use for linking .链接一般应该用于链接 Buttons can be used for other functionality you wish to add.按钮可用于您希望添加的其他功能。 Neals solution works, but its a workaround. Neals 解决方案有效,但它是一种解决方法。

If you use a <button> instead of a <a> , your original code should work as expected.如果您使用<button>而不是<a> ,您的原始代码应该会按预期工作。

When you click the text or link the image will be changed to another image so you can use the below script helps to you to change image on click the link:当您单击文本或链接时,图像将更改为另一个图像,因此您可以使用以下脚本帮助您在单击链接时更改图像:

<script>
$(document).ready(function(){
$('li').click(function(){
var imgpath = $(this).attr('dir');
$('#image').html('<img src='+imgpath+'>');
});
$('.btn').click(function(){
$('#thumbs').fadeIn(500);
$('#image').animate({marginTop:'10px'},200);
$(this).hide();
$('#hide').fadeIn('slow');
});
$('#hide').click(function(){
$('#thumbs').fadeOut(500,function (){
$('#image').animate({marginTop:'50px'},200);
});
$(this).hide();
$('#show').fadeIn('slow');
});
});
</script>




 <div class="sandiv">
    <h1 style="text-align:center;">The  Human  Body  Parts :</h1>
    <div id="thumbs">
    <div class="sanl">
    <ul>
    <li dir="5.png">Human-body-organ-diag-1</li>
    <li dir="4.png">Human-body-organ-diag-2</li>
    <li dir="3.png">Human-body-organ-diag-3</li>
    <li dir="2.png">Human-body-organ-diag-4</li>
    <li dir="1.png">Human-body-organ-diag-5</li>
    </ul>
    </div>
    </div>
    <div class="man">
    <div id="image">
    <img src="2.png" width="348" height="375"></div>
    </div>
    <div id="thumbs">
    <div class="sanr" >
    <ul>
    <li dir="5.png">Human-body-organ-diag-6</li>
    <li dir="4.png">Human-body-organ-diag-7</li>
    <li dir="3.png">Human-body-organ-diag-8</li>
    <li dir="2.png">Human-body-organ-diag-9</li>
    <li dir="1.png">Human-body-organ-diag-10</li>
    </ul>
    </div>
    </div>
    </div>

css: CSS:

<style>
body{ font-family:Tahoma, Geneva, sans-serif; color:#ccc; font-size:11px; margin:0; padding:0; background-color:#111111}
.sandiv{ width:980px;height:570px;margin:0 auto;margin-top:20px; padding:10px; background-color:#000;-webkit-box-shadow: 0 1px 2px #666;box-shadow: 0 1px 2px #666;}
#image{width:348px; height:375px; border-radius:100%;margin:0 auto; margin-top:50px; margin-bottom:20px;}
#thumb{width:400px;margin:0 auto; display:none;}
ul{list-style:none; padding:0; margin:0;}
 li{ width:auto ; height:50px; border-radius:100%; margin:5px; cursor:pointer; }
.sanl
{
 margin-top:50px;
 float:left;
 width:210px;
 margin-left:30px;
 margin-right:30px;
  }
.sanr
{
    margin-top:50px;
 float:left;
 width:210px;
 margin-left:60px;
 margin-right:30px;
}
.man
{
 float:left;
 width:350px;
 margin-left:30px;
 margin-right:30px; 
}
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM