在META代码中显示DIV中的内容 [英] Show content from DIV in a META-tag
问题描述
我有一个像这样的div:
< div id =beskrivelseclass =description html_content itemprop =description>
{{description}}
< / div>
内容 {{description}}
pre>
< meta property =og:descriptioncontent =HERE/>
如果我写的话语法错误:
< meta property =og:descriptioncontent ={{description}}/>
还有一个简单的方法吗?也许把它放在某种类型的变量中?
我没有办法改变这个在后端,因为我使用一个封闭的系统,Tictail.com。
我想让我在 {{description}}
中写的内容显示在元标记中。
我不能使用任何php,asp(etc)代码作为网站不允许这样。
有任何建议吗?
我没有成功尝试过:
< script language =javascripttype =text / javascript>
var descval = document.getElementById('beskrivelse')。innerHTML;
var paras = document.getElementsByTagName('meta');
for(i = 0; i var test = paras [i] .getAttribute('property');
if(test ==og:description)
{
paras [i] .content = descval;
}
}
< / script>
元素在你的DOM中有一个属性 og:description
,你运行你的脚本在页面底部或一个DOM准备回调。正如它在你的小提琴,如果你设置JS运行onDomready(像我在这里) 。
如果你从头开始在DOM中没有meta元素,你可能需要创建它并将它附加到DOM。类似这样的东西:
< script&
//获取内容
var descval = document.getElementById('beskrivelse')。innerHTML;
//创建新的元元素
var elm = document.createElement(meta);
elm.setAttribute(property,og:description)
elm.setAttribute(content,descval);
//将元素附加到HEAD
document.getElementsByTagName(head)[0] .appendChild(elm);
< / script>
I have a div which is like this:
<div id="beskrivelse" class="description html_content" itemprop="description">
{{description}}
</div>
The content {{description}}
is made in the backend.
How can I make it so that this content will be shown in my meta here:
<meta property="og:description" content=" HERE "/>
I get a syntax error if I write:
<meta property="og:description" content=" {{description}} "/>
Is ther a simple way to do it? Maybe put it in a variable of some sort?
There is no way for me to change this in the backend as I am using a closed system, Tictail.com.
I want what I have written in my {{description}}
to be shown in the meta tag.
I cannot use any php, asp (etc) code as the site doesnt allow this.
Any suggestions?
I have tried this with no succes:
<script language="javascript" type="text/javascript">
var descval = document.getElementById('beskrivelse').innerHTML;
var paras = document.getElementsByTagName('meta');
for (i = 0; i < paras.length; i++) {
var test = paras[i].getAttribute('property');
if(test == "og:description")
{
paras[i].content = descval;
}
}
</script>
Your code should work, given that you already have a meta-element in you DOM with a property og:description
and that you run you script either at the bottom of your page, or in a DOM-ready callback. As it does in your Fiddle, if you set the JS to be run onDomready (as I did here).
If you don't have the meta-element in the DOM from the beginning, you might need to create it and append it to the DOM. Something like this:
<script>
// Get the content
var descval = document.getElementById('beskrivelse').innerHTML;
// Create the new meta-element
var elm = document.createElement("meta");
elm.setAttribute("property", "og:description")
elm.setAttribute("content", descval);
// Append the element to the HEAD
document.getElementsByTagName("head")[0].appendChild(elm);
</script>
这篇关于在META代码中显示DIV中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!