使用 jQuery 设置元标记值时遇到问题
我正在尝试使用 jQuery 设置元标记(请不要回复说这没有意义,因为搜索引擎等等等等......我正在加载调用检查这些值的第 3 方 Javascript 函数,例如 Tweetmeme 和 Facebook)。
如果我使用:
$('meta[name=some-name]').attr('content', 'some value');
它确实可以设置现有元标记的值,但如果这样的命名标记不存在,则不会创建元标记。
如果您对此有见解或经验,请回复...
Possible Duplicate:
Is it possible to use javascript to change the meta-tags of the page?
I'm trying to set meta tags using jQuery (please don't reply that this doesn't make sense since search engines blah-blah-blah... I am loading invoking 3rd party Javascript functions that examine these vales, such as Tweetmeme and Facebook).
If I use:
$('meta[name=some-name]').attr('content', 'some value');
it does work to set the value of an existing meta tag, but does not create a meta tag if such a named one does not exist.
If you have insight or experience with this, please reply...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做,不是最干净的,但你想做的事情很奇怪,所以没有一个很好的方法来处理它:
其中的条件表达式检查
.length
,它是 0 或false
如果没有找到,如果是这种情况,我们创建元素并将其附加到头部,无论是添加的还是最初找到的,mt
都会设置为现在使用该名称的
即可设置该属性。You can do this, not the cleanest, but what you're trying to do is pretty odd so there's not a great way to handle it:
The conditional expression in there checks
.length
, which is 0 orfalse
if not found, if that's the case we create the element and append it to the head, whether it was added or originally found,mt
is set to the<meta>
with that name now, and you can set the attribute.利用尼克的代码,我创建了一个函数来进行元标记设置,并在必要时创建。这应该对任何人都有用...
元名称最常被假定为“名称”,但我还必须设置“属性”,因此使函数处理元元名称。
Leveraging Nick's code, I made a function to do the meta tag setting, creating if necessary. Should this be of use to anyone...
The metaName most often might be assumed to be "name" but I am also having to set "property" as well, so made the function handle a meta meta-name.
我只是在 iOS 上尝试了一下。事实证明,可以动态设置元标记来防止在 iOS 上缩放,如下所示:
但是删除它,并没有重新启用缩放:
但覆盖元标记:
使用其默认值可以。
I was just experimenting with this a bit on iOS. It turned out that it was possible to dynamically set a meta tag to prevent zooming on iOS like so:
Removing it however, didn't re-enable zooming:
But overwriting the meta tag:
with its default values did.