javascript - 根据父链接更改图像源?

标签 javascript html image src

我已经研究过此解决方案的 Javascript,但恐怕我对此的了解太有限了。我猜这个问题的解决方案必须用“节点”来解决,但我只是想不通如何...

我希望通过更改图像源来绕过以下网站上的图像删除。 Website link

<a href="/music/King+Crimson/In+the+Court+of+the+Crimson+King" class="g3 album-item-cover link-hook" itemprop="url">
   <div class="cover-image  cover-image--no-content" style="background-image: url('http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png');">
      <img class="cover-image-image" src="http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png" alt="" />
      <div class="text-over-image ">
         <span class="text-over-image-text">In the Court of the Crimson King</span>
         <br/>
         <span class="text-over-image-text text-over-image-text--secondary">148,881 listeners</span>
      </div>
   </div>
</a>

现在我尝试了非常基本的功能,例如“替换”。但我没有得到任何结果。有没有办法根据代码顶部的 URL 使用 JavaScript 更改以下内容?

例子:

<a href="/music/King+Crimson/URL-A">
...
<img class="cover-image-image" src="http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png" alt="" />
...
</a>

<a href="/music/King+Crimson/URL-B">
...
<img class="cover-image-image" src="http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png" alt="" />
...
</a>

进入:

<a href="/music/King+Crimson/URL-A">
...
<img class="cover-image-image" src="COVERFORA.JPG" alt="" />
...
</a>

<a href="/music/King+Crimson/URL-B">
...
<img class="cover-image-image" src="COVERFORB.JPG" alt="" />
...
</a>

对于我提供给自己的少量 Material ,我深表歉意,但我只是想知道是否真的有解决方案。 感谢您花时间阅读这篇文章。

干杯!

编辑:

来自以下网站的几个源代码示例:http://www.last.fm/music/King+Crimson/+albums

<a href="/music/King+Crimson/In+the+Court+of+the+Crimson+King"    class="g3 album-item-cover link-hook" itemprop="url">
    <img src="http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png" alt="Album cover for In the Court of the Crimson King" class="rounded" width="220" height="220" />
</a>

<a href="/music/King+Crimson/Red"    class="g3 album-item-cover link-hook" itemprop="url">
    <img src="http://cdn.last.fm/flatness/responsive/2/noimage/default_album_300_g4.png" alt="Album cover for Red" class="rounded" width="220" height="220" />
</a>

现在,我的想法是将我自己的图像源插入到每个链接中,但显然这更棘手,因为所有链接都具有与默认相同的图像。

最佳答案

不太确定你想做什么

var albumCover = document.querySelectorAll('.g3.album-item-cover.link-hook'), // get all links
    i = albumCover.length,
    a, img,
    dict = {  // link dictionary, href : src
        '/music/King+Crimson/In+the+Court+of+the+Crimson+King' : 'http://photobucket.com/InTheCourt.jpg',
        '/music/King+Crimson/Discipline' : 'http://photobucket.com/Discipline.jpg'
    };
while( --i >= 0 ) {                         // loop over each link
    a = albumCover[i];                      // that link
    img = a.getElementsByTagName('img')[0]; // that image
    img.src = dict[a.getAttribute('href')]  // replace src on that image based on that link
              || img.src;                   // else no change
}

关于javascript - 根据父链接更改图像源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112841/

相关文章:

javascript - 一种限制公共(public)访问的简单数据存储模式

html - document.body.scrollTop 在 IE 中始终为 0,即使在滚动时也是如此

javascript - 在 Tumblr 中嵌入 HTML5 视频(仪表板和博客)

css - 赋予虚拟图像自然的宽度和高度以做出响应

c++ - 在image-OpenCV和C++中添加高斯噪声然后去噪?

python - 如何从 python 中的模糊图像中找到扭曲矩形的准确角位置?

javascript - 如果 Nightmare waitTimeout超出,则调用函数

javascript - createContextualFragment 只是以字符串形式从字符串中吐出 html

javascript - 如何在 HERE Maps API for Javascript 中检索自定义标记坐标?

javascript - Slickgrid 移动外观