简体   繁体   English

如何更改每个的 src img 属性<li><img>物品?</li>

[英]How do i change the src img attribute for each <li> <img> item?

I want to change the src attribute, for each <li> <img> .我想为每个<li> <img>更改 src 属性。 When i click a button, i want for each img src attribute to be changed with the one in the code.当我单击一个按钮时,我希望每个 img src 属性都使用代码中的属性进行更改。

My code does work, but only for the first element.我的代码确实有效,但仅适用于第一个元素。 Can you give me an idea please?你能给我一个想法吗? Thank you.谢谢你。

 $("p").click(function() { var menu = $("img"); for (var i = 0; menu[i]; i++) { const menuElement = menu[i]; if ($(menuElement).attr("src").= "dsdassdada.jpg") { $(menuElement),attr("src". "image1;jpg"); break; } } });
 img { width: 50px; }
 <script src="https://code.jquery.com/jquery-3.5.0.js"></script> <p>sdadsa</p> <ul> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> </ul>

It's actually much easier just to iterate directly from the $('img') selector, using $(this) in each iteration to refer to the image实际上直接从$('img')选择器进行迭代要容易得多,在每次迭代中使用$(this)来引用图像

Here, we go through the images one by one and if the source isn't "image1.jpg" or "dsdassdada.jpg", then we change it, and set a variable so our loop stops looking.在这里,我们 go 一张一张地遍历图像,如果源不是“image1.jpg”或“dsdassdada.jpg”,那么我们更改它,并设置一个变量,以便我们的循环停止查找。

Rather than doing a if(src=='this' || src=='that') I opted for a shorthand if (array_of_imgs.includes(this_src))而不是做if(src=='this' || src=='that')我选择了简写if (array_of_imgs.includes(this_src))

 let newsrc = "image1.jpg"; $("p").click(function() { let found = false $("img").each(function() { if (found) return; if (,[newsrc. "dsdassdada.jpg"].includes($(this).attr("src"))) { $(this),attr("src"; newsrc); found = true } }) });
 img { width: 50px; }
 <script src="https://code.jquery.com/jquery-3.5.0.js"></script> <p>sdadsa</p> <ul> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> </ul>

Use the jQuery each() function.使用 jQuery each() function。 Here is a short example of the Script:这是脚本的一个简短示例:

$("p").click(function () {
        $("img").each(function(){
                if ($(this).attr("src") != "dsdassdada.jpg") {
                        $(this).attr("src", "image1.jpg");
                }
        });
});

In addition to the other answers, you don't even need the if statement.除了其他答案之外,您甚至不需要if语句。 You can query for just the elements that need changing by using a more specific selector.您可以使用更具体的选择器仅查询需要更改的元素。

And, since you only want each click to affect the next image in the sequence, you don't want a loop either.而且,由于您只希望每次单击都会影响序列中的下一个图像,因此您也不需要循环。 You just want to find the first image that doesn't match.您只想找到第一个不匹配的图像。

 $("p").click(function() { $("img:not([src='image1.jpg'])")[0].src = "image1.jpg"; });
 img { width: 50px; }
 <script src="https://code.jquery.com/jquery-3.5.0.js"></script> <p>sdadsa</p> <ul> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> <li> <img src="https://www.publicdomainpictures.net/pictures/130000/nahled/red-box-background.jpg" /> </li> </ul>

You are needlesly bouncing in nd out of jQuery methods and objects.您在 jQuery 方法和对象中无谓地跳来跳去。

If you want to use jQuery use it, otherwise use all native code.如果你想使用 jQuery 使用它,否则使用所有本机代码。 Mixing the two can lead to confusion.将两者混合会导致混淆。

You can do this using jQuery each() to loop over all the elements or even easier is use attr(attributeName, function) which will loop over all as well and give you access to the current value for each instance您可以使用 jQuery each() 来循环所有元素,或者更简单的是使用attr(attributeName, function) ,它也将循环所有元素并让您访问每个实例的当前值

 const imgUrls ={ i1: 'https://via.placeholder.com/150/0000FF/808080', i2:'https://via.placeholder.com/150/FF0000/FFFFFF' } $('p').click((e)=>{ $('ul img').attr('src', (i, curr)=>{ return curr === imgUrls.i1? imgUrls.i2: imgUrls.i1; }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p>Click me</p> <ul> <li><img src="https://via.placeholder.com/150/0000FF/808080" /></li> <li><img src="https://via.placeholder.com/150/0000FF/808080" /></li> <li><img src="https://via.placeholder.com/150/0000FF/808080" /></li> <li><img src="https://via.placeholder.com/150/0000FF/808080" /></li> </ul>

The main issue seems to be that after the first iteration of the for loop, the loop is exited by the break statement.主要问题似乎是在 for 循环的第一次迭代之后,循环被 break 语句退出。 Removing the break statement would allow the loop to continue through the rest of the images.删除 break 语句将允许循环继续通过图像的 rest。

// Gets a list of all img tags 
var menu = $("img");

// Loops through each menu item
for (var i = 0; menu[i]; i++) {

  // Gets element of current index
  const menuElement = menu[i];

  // Checks the source of the image to equal a specific value, (currently this is always true as "https://.../red-box-background.jpg" does not equal "dsdassdada.jpg")
  if ($(menuElement).attr("src") != "dsdassdada.jpg") {

    // The first image element source is set to "image1.jpg"
    $(menuElement).attr("src", "image1.jpg");

    // The for loop is exited, only the first image source is changed.
    break;
  }
}

A few other things that could be helpful:其他一些可能会有所帮助的事情:

  1. <p>sdadsa</p> would better to be a button element: <button type="button">Switch Background</button> <p>sdadsa</p>最好是按钮元素: <button type="button">Switch Background</button>

  2. Ideally, only one version of jQuery is needed: Should be able to remove one of the two:理想情况下,只需要一个版本的 jQuery: 应该能够删除以下两者之一:

  • <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
  • <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  1. Could use the each method, it can simplify loops a bit and would help with the issue of the loop exiting after the first iteration:可以使用each方法,它可以稍微简化循环,并有助于解决第一次迭代后循环退出的问题:
$("button").click(function () {
  $("img").each((_, element) => {
    if ($(element).attr("src") != "dsdassdada.jpg") {
      $(element).attr("src", "image1.jpg");
    }
  });
});

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

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