2016-12-30 24 views
0

如何更改嵌入在錨標記中的圖像源。更改錨標記中使用純javascript的圖像源不是jquery

這是代碼。

<body class="home"> 
    <a href="#" class="logo"><img src="https://storage.googleapis.com/gweb-   
     uniblog-publish-prod/static/blog/images/google-200x200.7714256da16f.png"/></a 
    </body> 
+1

你至今沒有嘗試過任何ID ??? –

+0

正在嘗試通過 –

+0

正在嘗試通過document.getelementbyclassName但我無法得到如何combile都獲得elementbyclassname和標記名 –

回答

0
<a href="newimage.jpg" onclick="this.children[0].src = this.href; return false;" class="logo"><img src="oldimage.jpg"/></a> 
0

添加idimg標籤,然後使用下面的代碼。

<script> 
document.getElementById("newImage").src="newImagepath/newimage.jpg"; 
</script> 
0

你可以給這個img標籤的ID,然後使用

document.getElementById(id).src = "newimagesource.png" ; 
0

嘗試使用querySelector這樣的 -

document.querySelector(".logo img").src = "your new src"; 
+0

感謝它的工作 –

-1

你可以這樣說:

var img = document.querySelector('.logo img'); 
    img.src = 'your new source'; 
0
<body class="home"> 
    <a href="#" class="logo"><img id ="picture" src="https://storage.googleapis.com/gweb-   
     uniblog-publish-prod/static/blog/images/google-200x200.7714256da16f.png"/></a 
    </body> 

的Javascript:

<script type="text/javascript"> 
     document.getElementsById("picture").setAttribute("src", "New_image_path"); 
</script> 
0

做它最小的方式是

document.querySelector(".logo img").src = "new_source"; 
0
Just assign the id attribute to the image and change the source, 
for your case: 

<body class="home" onload="changethesrc('newimagescr.png')"> 
 
     <a href="#" class="logo"> 
 
     <img src="https://storage.googleapis.com/gweb-   
 
     uniblog-publish-prod/static/blog/images/google-200x200.7714256da16f.png" id="imageid"/></a 
 
     </body> 
 
     <script type="text/javascript"> 
 
    \t  function changethesrc(newsrc){ 
 
    \t \t var tmpimg = document.getElementById("imageid"); 
 

 
\t \t tmpimg.src = newsrc; 
 
    \t  } 
 
     </script>

0

試試這個

document.getElementsByClassName("logo")[0].children[0].src="http://images.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png" 

不使用我創建了一個小提琴 https://jsfiddle.net/f3rdnzgt/1/