Javascript 从没有名称的元素中选择属性
我的HTML中有这个Javascript 从没有名称的元素中选择属性,javascript,Javascript,我的HTML中有这个 <meta name="title" content="Hello World"/> <meta property="article:published_time" content="2014-11-20T11:00:01+00:00"/> 显示为未定义。我尝试用“\\”返回一个。可以使用['xyz']表示法访问集合的元素,但这将查找id为'xyz'的元素,否则将查找名为'xyz'的元素。集合的元素也可以通过整数索引访问,如下所示 第一个示例之所以
<meta name="title" content="Hello World"/>
<meta property="article:published_time" content="2014-11-20T11:00:01+00:00"/>
显示为未定义。我尝试用“\\”返回一个。可以使用['xyz']表示法访问集合的元素,但这将查找id为'xyz'的元素,否则将查找名为'xyz'的元素。集合的元素也可以通过整数索引访问,如下所示
第一个示例之所以有效,是因为您有一个名为“title”的元元素。但第二个示例没有名称或id。因此,您必须在集合中循环:
var pub_time, collection = document.getElementsByTagName("meta");
for(var i = 0;i < collection.length;i++) {
if(collection[i].getAttribute("property") === "article:published_time") {
pub_time = collection[i].getAttribute("content");
}
var pub_time,collection=document.getElementsByTagName(“meta”);
for(var i=0;i
有关HTMLCollection
的更多信息,请参见此处:您正在查找此
尝试使用单个“`。尝试document
document.getElementsByTagName(“meta”)[1]['article:published\u time']
var pub_time, collection = document.getElementsByTagName("meta");
for(var i = 0;i < collection.length;i++) {
if(collection[i].getAttribute("property") === "article:published_time") {
pub_time = collection[i].getAttribute("content");
}
$('meta[property="article:published_time"]').attr('content')