简体   繁体   English

更改嵌套alt标签中的img

[英]Changing the img in a nested alt tag

How can I programmatically change the following img in the structure below? 如何在下面的结构中以编程方式更改以下img

  <div id="runner" class="nav brand pull-left">
        <a alt="motherboard" href="/tree/">
            <img alt="mega node" src="img.png"></img>
        </a>
  </div>

firefox points me to this firefox指出了这一点

#runner > a:nth-child(1) > img:nth-child(1)

I've tried using standard methods as the following with various naming conventions 我尝试使用以下标准方法和各种命名约定

document.getElementById("#runner").src="icon.png";
document.getElementById("#runner.a.img").src="icon.png";
document.getElementById("#runner.img").src="icon.png";
document.getElementById("mega node").src="icon.png";

I've even tried css with no avail, 我什至没有尝试过CSS,

div#runner {
    content:url("img.png");
}

Your are specifying argument in getElementById() in a wrong way, you should provide just an id , without selector # . 您在getElementById()中以错误的方式指定参数,您应该只提供一个id ,而不使用选择器# You can find your #runner div and then find the first <img> tag in it and change it's src attribute : 您可以找到#runner div ,然后在其中找到第一个<img>标记并更改其src属性:

  document.getElementById('runner').querySelector('img').src = 'icon.png' 

Example

Or even simpler, without using getElementById() : 甚至更简单,无需使用getElementById()

 document.querySelector('#runner a img').src = 'icon.png'

Example

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

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