将 Js 变量转为元数据
Turn Js Variable into Meta data
在 head 部分我有一个 js:
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
有没有办法把 ProductID 变成像
这样的 Meta 标签
<meta name="brand" content="97087">
这可以通过遍历产品对象的键并将其与关联值附加到 <head>
来实现。下面的代码将导致:
<meta name="ProductID" content="97087">
<meta name="Thema" content="18">
<meta name="CategoryID" content="49">
<meta name="Region1" content="21">
<meta name="Region2" content="35">
<meta name="Region3" content="">
<meta name="Price" content="22,00">
<meta name="Brand" content="Brand">
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
var metaTags = Object.keys(product).map(function(key) {
return '<meta name="' + key + '" content="' + product[key] + '">';
}).join('');
var headElem = document.querySelector('head');
headElem.innerHTML += metaTags;
console.log(headElem.querySelectorAll('meta'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>
保存此代码段以进行测试
只需使用 JS 创建元标记并将其附加到文档的头部
运行 它并右键单击以检查 head 标签以查看元数据已创建
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
//<meta name="brand" content="97087">
var meta = document.createElement('meta');
meta.setAttribute('name',product.Brand);
meta.setAttribute('content',product.ProductID);
document.getElementsByTagName('head')[0].appendChild(meta);
在 head 部分我有一个 js:
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
有没有办法把 ProductID 变成像
这样的 Meta 标签<meta name="brand" content="97087">
这可以通过遍历产品对象的键并将其与关联值附加到 <head>
来实现。下面的代码将导致:
<meta name="ProductID" content="97087">
<meta name="Thema" content="18">
<meta name="CategoryID" content="49">
<meta name="Region1" content="21">
<meta name="Region2" content="35">
<meta name="Region3" content="">
<meta name="Price" content="22,00">
<meta name="Brand" content="Brand">
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
var metaTags = Object.keys(product).map(function(key) {
return '<meta name="' + key + '" content="' + product[key] + '">';
}).join('');
var headElem = document.querySelector('head');
headElem.innerHTML += metaTags;
console.log(headElem.querySelectorAll('meta'));
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
</html>
保存此代码段以进行测试
只需使用 JS 创建元标记并将其附加到文档的头部
运行 它并右键单击以检查 head 标签以查看元数据已创建
var product = {
ProductID: '97087',
Thema: '18',
CategoryID: '49',
Region1: ['21'],
Region2: ['35'],
Region3: [],
Price: '22,00',
Brand: 'Brand',
};
//<meta name="brand" content="97087">
var meta = document.createElement('meta');
meta.setAttribute('name',product.Brand);
meta.setAttribute('content',product.ProductID);
document.getElementsByTagName('head')[0].appendChild(meta);