How to set Attribute value for a class with multiple items

This might be quiet simple though, but I am just some few months old in web development and i got dead stuck here. I have a page with multiple image items set to one class. By this, I meant the following code

<img src="http://img1.gif" id="GA2" class="CHAngeR" />    
<img src="http://img1.gif" id="GA2" class="CHAngeR" />

and i want to execute a function that edits/appends the "src" attribute of the images at ones.

I tried using the same Id, but i discovered that w3c speculations are contrary, i also tried the following code but still doesn't work, how can i do this.

 <!DOCTYPE html>
    <html>
       <head>
          <title>DOWNLOAD PAGE</title>
          <input type="button" value="change image" id="iMgChAnGe" onclick="changeIMG()"/>
          <div class="changer">
               <img src="http://img1.gif" id="GA1" class="CHAngeR" />
               <img src="http://img2.gif" id="GA2" class="CHAngeR" />
               <img src="http://img3.gif" id="GA3" class="CHAngeR" />
               <img src="http://img4.gif" id="GA4" class="CHAngeR" />
               <img src="http://imgl.gif" id="GA5" class="CHAngeR" />
          </div>
          <script>
             function ChangeIMG(){
                 document.getElementByClassName(CHAngeR).setAttribute("src","http://google.com/img/images/static.gif");
             } 
          </script>
      </head>
</html>

Loop through the images manually...

var imgs = document.getElementsByClassName("...");
for (var i = 0; i < imgs.length; i++)
    imgs[i].src = ...;

or use jQuery:

$(".class").attr("src", ...);

I don't think getElementsByClassName is supported by all browsers (IE). That being said, I recommend using a JQuery solution.

Go here if you can use JQuery.

Go here if for some reason you don't want to use JQuery.

链接地址: http://www.djcxy.com/p/83518.html

上一篇: 带有Asp.Net核心ViewComponents的多个Knockout视图模型

下一篇: 如何为具有多个项目的类设置属性值