简体   繁体   English

使用jquery,我如何选择div内的所有图像来改变它的来源

[英]using jquery, how can i select all images inside of a div to change its source

I have the following div and I know the selector Id of the DIV. 我有以下div,我知道DIV的选择器ID。

<div class="event">
  <img src="/Content/Images/Icons/calendar1.png">
  <img src="/Content/Images/Icons/calendar2.png">
  <img src="/Content/Images/Icons/calendar3.png">
  <img src="/Content/Images/Icons/calendar4.png">
  <img src="/Content/Images/Icons/calendar5.png">
  <img src="/Content/Images/Icons/calendar6.png">
</div>

I need something to find all images selector inside the div that i have so i can go change the source of the each image to a new image. 我需要一些东西来找到我所拥有的div内的所有图像选择器,所以我可以将每个图像的源更改为新图像。

Use this code to retrieve the image URL: 使用此代码检索图像URL:

$('.event img').each(function(){
  alert($(this).attr('src'));
});

if you want to change the image URL then use this: 如果您想更改图片网址,请使用以下内容:

var inc = 1;
$('.event img').each(function(){
  $(this).attr('src','path of image/imagename'+inc+'.imageextension');
  inc++;
});

You can try children selector and attr callback together. 您可以一起尝试子选择器和attr回调。

  $(".event img").attr("src", function() { // or id return "Something/" + $(this).attr("src"); }); console.log($(".event").html()); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="event"> <img src="/Content/Images/Icons/calendar1.png"> <img src="/Content/Images/Icons/calendar2.png"> <img src="/Content/Images/Icons/calendar3.png"> <img src="/Content/Images/Icons/calendar4.png"> <img src="/Content/Images/Icons/calendar5.png"> <img src="/Content/Images/Icons/calendar6.png"> </div> 

You can loop through each element like this: 您可以像这样遍历每个元素:

i represent the index of each element, I've added this incase you want something like i代表每个元素的索引,我已经添加了这个你想要的东西

imgurl1 imgulr2 imgurl3 imgurl1 imgulr2 imgurl3

and so on 等等

 $(".event img").each(function(i, x) { $(this).attr("src", "/Content/Images/Icons/NewURL" + (i + 1) + ".png") console.log($(this).attr("src")) }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="event"> <img src="/Content/Images/Icons/calendar1.png"> <img src="/Content/Images/Icons/calendar2.png"> <img src="/Content/Images/Icons/calendar3.png"> <img src="/Content/Images/Icons/calendar4.png"> <img src="/Content/Images/Icons/calendar5.png"> <img src="/Content/Images/Icons/calendar6.png"> </div> 

您可以按照代码段返回img元素列表,

$('.event').find('img');

change all to same: 将所有内容改为相同:

$(".event>img").attr("src","/New/Image.jpg");

change all to something: 改变一切:

$(".event>img").each(function(index){
    $(this).attr("src",variable);
});

Use array selector For that : img[0] , img[1] And .Attr method ("src","url"); 使用数组选择器为此: img[0] , img[1].Attr方法("src","url"); Or you can Just give Them class to select the each img !! 或者你可以给他们的课程来选择每个img !!

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

相关问题 如何选择 div 内的图像来更改其来源? - How can I select an image inside of a div to change its source? 如何使用 jquery 在 div 内传播图像 - how can i spread images inside div using jquery 如何使用jQuery为div中的所有图像添加类? - How to Add class to all images inside div using jQuery? 如何使用jQuery在鼠标移动时更改div中的图像源? - How can I change image source in a div on mouse move with jquery? 如何在div中选择具有JQUERY中特定属性的元素? - How can I select an element inside a div with a specific attribute in JQUERY? 如何使用jQuery选择div内的元素 - How do I select an element inside of a div using Jquery 如何使用jQuery更改A元素中DIV的类? - How can I use jQuery to change the class of a DIV inside an A element? 如何使用jquery或javascript从div元素中删除所有以#开头的字符? - How can i remove all characters started with # from inside a div element using jquery or javascript? 如何使用jquery在画布中更改图像源? - How can I change the image source within a canvas using jquery? 使用jQuery,我如何将鼠标悬停在一个元素上并使其其余兄弟姐妹更改大小? 没有DIV下降到下一行? - Using jQuery how can i on hover make an element active and make the rest of its siblings change sizes? without DIV dropping to the next line?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM