首页 > 解决方案 > 动态更改标题中的元标记?

问题描述

我正在使用一个按钮创建一个可共享的连接,我之前单击的按钮将执行一项功能,该功能在将 OPEN GRAPH 发布到社交媒体后对其进行修改。我尝试单击按钮并使用共享 API 与社交媒体共享,但标题、链接、缩略图和描述没有改变。

const share = $("#change").click(()=>{
   $("meta[name=description]").remove();
   $("head").append("<meta name='description' content='new description'>" 
);})

标签: javascriptjquerycsswebdynamic

解决方案


您可以使用此更改元标记

$('button').on('click', function() {
    $('meta[name=description]').remove();
    $('head').append( '<meta name="description" content="this is new">' );
    console.log($('meta[name=description]').attr('content'));
});
<head>
    <meta name="description"content="this is old">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<button>Change Meta</button>


推荐阅读