使用 jQuery 动态更改元标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7308970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 23:41:06  来源:igfitidea点击:

Changing meta-tags dynamic with jQuery

jqueryhtmlmeta-tags

提问by OptimusCrime

These are my meta-tags:

这些是我的元标签:

<meta property="og:image" content="assets/css/gfx/skold.png"/>
<meta property="og:title" content="Den historie hjemmesiden for Norges Golfforbund"/>
<meta property="og:description" content="Her finner du alle de historie tingene som har skjedd i Norges golfhistorie gjennom tidene" />
<meta property="og:url" content="http://###"/>
<meta property="og:site_name" content="Norges Golfklubb"/>
<meta property="og:type" content="sport"/>

And I am trying to change them dynamic with the following code:

我正在尝试使用以下代码动态更改它们:

$("meta[property=og:title]").attr("content", result.title);

But I am keep getting Syntax error, unrecognized expression: [property=og:title] in Firebug.

但我不断收到语法错误,无法识别的表达式:[property=og:title] 在 Firebug 中。

Using the latest version of jQuery. Does anyone know what I am doing wrong?

使用最新版本的 jQuery。有谁知道我做错了什么?

回答by Nicola Peluchetti

I think you should escape :look at the documentation

我认为你应该逃避:文档

$("meta[property='og\:title']").attr("content", result.title);

回答by Curtis

$("meta[name='og:title']").attr('content', 'my new title');

Using "property=" does not work in Chrome

在 Chrome 中使用“property=”不起作用

回答by KDW

Make sure your quotes are like this:

确保你的报价是这样的:

$('meta[property="og:description"]').attr('content',"$modified_desc" );

回答by Frankey

You could give each meta element an ID or Class.

你可以给每个元元素一个 ID 或类。

$('#metaelement').attr('content', 'my new meta description');