简体   繁体   English

使用Java脚本替换部分元数据内容网址

[英]Replace part of a metadata content url using Javascript

Here is my raw HTML: 这是我的原始HTML:

<meta name='twitter:image' expr:content='data:blog.postImageThumbnailUrl'/>

And here is the result on the page 这是页面上的结果

<meta name="twitter:image" content="http://1.bp.blogspot.com/-DmLY0uHInEw/Ut45x9vmxAI/AAAAAAAADlw/4O6366ds6G0/s72-c/End_of_the_World_as_we_know_it.png">

Here is my javascript (which I'm sure is full of mistakes) 这是我的javascript(我肯定有很多错误)

$('meta').each(function () {
var href = $(this).attr('content').replace("s72-c", "s1600");
});

What I want to do: I need to replace s72-c in my metadata content url with s1600 . 我想做什么:我需要用s1600替换元数据内容URL中的s72-c

I've tried everything, but it's not working and I'm not very good with jquery :/ Can someone please help me out? 我已经尝试了所有方法,但是它不起作用,并且对jquery的使用不是很好:/有人可以帮帮我吗?

You've forgotten to actually set the new content attribute. 您忘记了实际设置新的content属性。 Try this: 尝试这个:

$(function() {
    $('meta').each(function () {
        var $this = $(this),
            href = $this.attr('content').replace("s72-c", "s1600");

        // now set it 
        $this.attr('content', href); 
    });
});

您可以使用一行代码来实现此目的,而无需每个函数:

$("meta").attr("content", function(){return $(this).attr("content").replace("s72-c", "s1600")});

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

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