简体   繁体   English

在jQuery中使用.click时如何设置src属性以更改图像

[英]How to set the src property to change an image when using .click in jQuery

I am trying to change an image using jquery upon clicking. 我试图在单击时使用jquery更改图像。 So when I click on an image it appears on the viewport then I click on another image above it and it changes to that image and then a third. 因此,当我单击图像时,它会出现在视口中,然后单击它上方的另一幅图像,然后它会变为该图像,然后变为第三幅。 When I try it only clicks on one image. 当我尝试时,它仅单击一张图像。

HTML CODE: HTML代码:

<div id="vacationimages">
<p>Click on the Image that best represents the kind of vacation you want</p>
<p><img src="mountain.jpg" alt="Mountain Vacation"><br /><br /></p>
<p><img src="desert.jpg" alt="Desert Vacation"><br /><br /></p>
<p><img src="ocean.jpg" alt="Ocean Vacation"><br /><br /></p>
</div>

<div id="bigimage">
<img id="currentimage" src="ocean.jpg" alt="ocean vacation">
<p id="imagedesc"></p>
</div>

jQuery Code: jQuery代码:

  $("#vacationimages", "src").click(function(){

 $("#firstname").text("");

 $("#bigimage").show("slow");

   var myfirst = $("#firstname").val();

  $("#currentimage").attr("src", "ocean.jpg");

   $("#currentimage").attr("src", "desert.jpg");

Only the 2nd image displays despite me having different images on the same page. 尽管我在同一页面上有不同的图像,但仅显示第二张图像。

Use "on" vs. "click" because it will then work for dynamically added elements, and save you headaches later. 使用“ on”与“ click”,因为它随后将适用于动态添加的元素,并为以后省去了很多麻烦。 After that it's simply populating the image properties with those you gathered from the clicked image or $(this) in the scope of your function. 之后,只需使用从单击的图像或函数范围内的$(this)收集的图像属性填充图像属性。

 $(function() { $("#vacationimages img").on("click", function() { $("#currentimage").attr("src", $(this).attr("src")); $("#imagedesc").html($(this).attr("alt")); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="vacationimages"> <p>Click on the Image that best represents the kind of vacation you want</p> <img src="http://fillmurray.com/30/30.jpg" alt="Mountain Vacation"> <img src="http://fillmurray.com/32/32.jpg" alt="Desert Vacation"> <img src="http://fillmurray.com/34/34.jpg" alt="Ocean Vacation"> </div> <div id="bigimage"> <img id="currentimage" src="" alt=""> <p id="imagedesc"></p> </div> 

Another common approach in a scenario like this is to use an anchor tag around the img ( <a> ) and populate it's rel attribute with a larger source image, if what you're going for is click a thumbnail to view a larger image. 在这种情况下,另一种常见的方法是在img( <a> )周围使用定位标记,并使用较大的源图像填充rel属性,如果要使用的是单击缩略图以查看较大的图像。 In that case you bind the event listener to the anchor (not the image) and change the src to the anchor's rel (as opposed to the src of the img inside the anchor). 在那种情况下,您将事件侦听器绑定到锚点(而不是图像),并将src更改为锚点的rel (与锚点内img的src相对)。

 $("#vacationimages img").on("click",function(){ $("#bigimage img").attr("src",$(this).attr("src")); $("#bigimage p").text($(this).attr("alt")); }); $("#submitme").on("click",function(){ $("#mymessage").text($("#name").val()+" You want a "+$("#imagedesc").text()); }); 
 img { height:32px; width:32px } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="vacationimages"> <p>Click on the Image that best represents the kind of vacation you want</p> <label>Name:<input type="text" id="name"></label><br><br> <img src="https://image.flaticon.com/icons/png/128/181/181549.png" alt="Mountain Vacation"><img src="https://image.flaticon.com/icons/png/128/148/148766.png" alt="Desert Vacation"> <img src="https://image.flaticon.com/icons/png/128/138/138662.png" alt="Ocean Vacation"> </div> <div id="bigimage"> <img id="currentimage" src="ocean.jpg" alt="ocean vacation"> <p id="imagedesc"></p> </div> <br><br> <div id="submitdiv"> <input id="submitme" type="button" value="Submit ME"> <p id="mymessage"></p> </div> 

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

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