簡體   English   中英

替換所有圖片“

[英]Replace all image “<a href” url, with the picture url from “<img src”

我使用wordpress,並且有rss發布問題。 一些具有外部帖子網址的鏈接圖片,如果用戶單擊,則打開外部網站。

我嘗試用php源代碼替換所有圖像鏈接,但是沒有運氣。

在示例中,我有2張圖片的帖子:

"blah blah blah"
<a href="http://google.com/thank-you.html"><img src="http://yahoo.com/img/top.jpg" alt="" /></a>
"blah blah blah"
<a href="http://google.com/index.html"><img src="http://yahoo.com/img/bottom.jpg" alt="" /></a>

我想將其轉換為:

"blah blah blah"
<a href="http://yahoo.com/img/top.jpg"><img src="http://yahoo.com/img/top.jpg" alt="" /></a>
"blah blah blah"
<a href="http://yahoo.com/img/bottom.jpg"><img src="http://yahoo.com/img/bottom.jpg" alt="" /></a>

也可以將img鏈接一一替換為php。

謝謝

嘗試這樣的事情

    jQuery('.img').each(function(){
        jQuery(this).parent('a').attr('href', this.src);
    });

HTML

    <img class="img" src="http://yahoo.com/img/top.jpg" alt="" />

試試這個:

<script>
var getSrc = function(imgSource) {
var img = new Image();
img.src = imgSource;
return img.src;
};
$( "a" ).attr({
href: getSrc,
});
</script>

嘗試這個

HTML

<a href="http://google.com/thank-you.html"><img class="image" src="http://yahoo.com/img/top.jpg" alt="dsfds" /></a>
<a href="http://google.com/index.html"><img class="image" src="http://yahoo.com/img/bottom.jpg" alt="ad" /></a>

jQuery的:

jQuery('.image').each(function(){
    var src = jQuery(this).attr('src');
    jQuery(this).parent('a').attr('href', src);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM