JavaScript更改元值 [英] Javascript change meta values

查看:91
本文介绍了JavaScript更改元值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Google加号按钮. google plus返回的值是基于我的head标签上的元数据中包含的内容

I'm currently working with google plus button. The values return by google plus is based on what is included at the meta on my head tag

我有一个要使用g + 1提交的列表,但它位于标签下,这就是为什么我无法更新将要在google plus上发布的内容的原因

And I have a list to be submitted using g+1 but it is under the tags that's why I can't update what is going to be posted on google plus

这些是我的密码

<meta itemprop="name" content="<?=$content?>">
<meta itemprop="description" content="<?=$description?>">
<meta itemprop="image" content="http://mysite/image.png">

在这些行下面是

    <div> content to submit by g+1
        <div class='g-plusone' data-size='medium' data-annotation='none'></div>
    </div>

    <div> content to submit by g+1
        <div class='g-plusone' data-size='medium' data-annotation='none'></div>
    </div>

    <div> content to submit by g+1
        <div class='g-plusone' data-size='medium' data-annotation='none'></div>
    </div>

 etc.

如何使用javascript,因此,每当鼠标悬停在google加号按钮上时,我都可以更新meta的值是什么?

how can I use javascript, so whenever the mouse hover on the google plus button, I can update what is the value on my meta?

喜欢

<div onmouseover='change_meta(x)' class='g-plusone' data-size='medium' data-annotation='none' ></div>

我已经知道如何在javascript函数上传递参数.我想知道的是,这些函数需要包含什么内容,以便更新元值.

I already know how to pass argument on javascript functions. What I want to know is, what do I need to include inside that functions so I can update the meta values.

<script language="JavaScript">
function change_meta(x) {
   what do i need to do here?
}
</script>

推荐答案

从服务器读取html的任何人都不会拾取您在浏览器上更改的任何元标记标签.您需要更改服务器上的meta标记才能使其生效,除非所涉及的站点使用的是实际上读取该标记的当前内容的书签.您使用的格式似乎是Google专有的微数据格式

Any meta tag tag you change on your browser will not be picked up by anyone reading the html from your server. You need to change the meta tag on the server for it to take effect unless the site in question is using a bookmarklet that actually reads the current content of the tag. The format you use seems to be a google proprietary microdata format

也就是说,如果您需要更改标签,就可以

That said, if you need to change the tag you can do

var metas = document.getElementsByTagName("meta");

并循环查找所需的标签:

and loop through finding the tag you want:

for (var i=0; i<metas.length; i++) {  
  if (metas[i].getAttribute("itemprop") && 
      metas[i].getAttribute("itemprop")==="description") {
    metas[i].setAttribute("content","new description");
  }
}

这篇关于JavaScript更改元值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆