简体   繁体   English

根据父链接更改图像源?

[英]Changing the image source depending on the parent link?

I've looked into Javascript for this solution but I'm afraid my knowledge for this is just too limited. 我已经研究过Javascript来解决这个问题,但是我对此的知识很有限。 I'm guessing the solution to this issue would have to be solved with "nodes" but I just can't wrap my mind around how ... 我猜想这个问题的解决方案将不得不用“节点”来解决,但我只是无法确定如何解决 ...

I'm looking to bypass image removal on the following website by changing the image source. 我希望通过更改图片来源来绕过以下网站上的图片删除。 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>

Now I've tried very basic functions like 'replace.' 现在,我尝试了非常基本的功能,例如“替换”。 but I got no results for that. 但是我没有结果。 Is there a way to change the following,using JavaScript, depending on the URL at the top of the code? 有没有一种方法可以根据代码顶部的URL使用JavaScript更改以下内容?

Example: 例:

<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>

Into: 进入:

<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>

I apologize for the little material I'm offering myself but I'm just wondering if there's actually a solution to this. 我为自己提供的少量材料表示歉意,但我只是想知道是否确实有解决方案。 Thanks for taking the time to read this post. 感谢您抽出宝贵的时间阅读这篇文章。

Cheers! 干杯!

Edit: 编辑:

A few examples of the source code from the following website: http://www.last.fm/music/King+Crimson/+albums 以下网站提供了一些源代码示例: 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>

Now, the idea is to insert my own image sources into each of these but obviously it's trickier because all of the links have the same image as default. 现在,想法是将我自己的图像源插入每个图像源中,但是显然这比较棘手,因为所有链接都具有默认图像。

$('a.album-item-cover > img.rounded'​).each(function(e) {
    var $this=$(this);
    $this.attr('src',this.parentElement.href
                          .replace(/^.*\/([^\/]+)$/,'Cover_for-$1.jpg'));
    }
    )​​​​​;

$1 here is string after last / $1是最后一个字符串/

Not entirely sure what you want to do 不确定您要做什么

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
}

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

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