更改嵌套alt标记中的img

pyC*_*hon 0 html javascript css css-selectors css3

如何以编程方式更改以下img结构中的以下内容?

  <div id="runner" class="nav brand pull-left">
        <a alt="motherboard" href="/tree/">
            <img alt="mega node" src="img.png"></img>
        </a>
  </div>
Run Code Online (Sandbox Code Playgroud)

firefox指向我这个

#runner > a:nth-child(1) > img:nth-child(1)
Run Code Online (Sandbox Code Playgroud)

我已尝试使用标准方法,具有各种命名约定

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";
Run Code Online (Sandbox Code Playgroud)

我甚至尝试过无用的CSS,

div#runner {
    content:url("img.png");
}
Run Code Online (Sandbox Code Playgroud)

pot*_*hin 5

getElementById()以错误的方式指定参数,你应该只提供一个id没有选择器#.你可以找到你的#runner div,然后<img>在其中找到第一个标签并更改它的src属性:

  document.getElementById('runner').querySelector('img').src = 'icon.png' 
Run Code Online (Sandbox Code Playgroud)

甚至更简单,不使用getElementById():

 document.querySelector('#runner a img').src = 'icon.png'
Run Code Online (Sandbox Code Playgroud)