[英]Change the image source in anchor tag using pure javascript not jquery
How can I change the image source which is embeded in anchor tag. 如何更改嵌入锚标记中的图像源。
Here is the code. 这是代码。
<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>
<a href="newimage.jpg" onclick="this.children[0].src = this.href; return false;" class="logo"><img src="oldimage.jpg"/></a>
Add id
to img
tag , then use following code. 将id
添加到img
标签,然后使用以下代码。
<script>
document.getElementById("newImage").src="newImagepath/newimage.jpg";
</script>
您可以给此img标签指定一个ID,然后使用
document.getElementById(id).src = "newimagesource.png" ;
尝试像这样使用querySelector
document.querySelector(".logo img").src = "your new src";
<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: 使用Javascript:
<script type="text/javascript">
document.getElementsById("picture").setAttribute("src", "New_image_path");
</script>
最小的方法是
document.querySelector(".logo img").src = "new_source";
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"> function changethesrc(newsrc){ var tmpimg = document.getElementById("imageid"); tmpimg.src = newsrc; } </script>
Try this 尝试这个
document.getElementsByClassName("logo")[0].children[0].src="http://images.google.com/images/branding/googleg/1x/googleg_standard_color_128dp.png"
without using any id i have created a fiddle https://jsfiddle.net/f3rdnzgt/1/ 不使用任何ID我已经创建了一个小提琴https://jsfiddle.net/f3rdnzgt/1/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.