简体   繁体   English

在jQuery中,为什么查询“ meta [name]”和“ meta [http-equiv]”时“ this”的值不同?

[英]in jQuery, why is the value of “this” when querying 'meta[name]' different from 'meta[http-equiv]' different?

Given: 鉴于:

<meta name="someKindOfId" value="asdf" />
<meta http-equiv="Set-Cookie" content="cookie_value1" />

and

var meta1 = $('meta[name]').filter(function() {
    console.log(this['name']);
});

var meta2 = $('meta[http-equiv]').filter(function() {
    console.log(this['http-equiv']);
});

The first console.log outputs someKindOfId (correct). 第一个console.log输出someKindOfId (正确)。

But the second console.log outputs undefined twice, instead of Set-Cookie ? 但是第二个console.log输出undefined两次,而不是Set-Cookie

Why is this so? 为什么会这样呢?

Because name is a property of dom element object where are http-equiv is not, it is an attribute value which is not copied as a property so try 由于name是dom元素对象的属性,而http-equiv不是,因此它是一个属性值,不会复制为属性,因此请尝试

var meta2 = $('meta[http-equiv]').filter(function() {
    console.log($(this).attr('http-equiv'));
});

Attributes like name , id , value has corresponding property matches in the dom element reference, so those values will get copied to the properties of the dom element reference and this inside the filter refers to the dom element 属性,如nameidvalue具有相应的属性中的DOM元素参考相匹配,因此这些值将会被复制到的DOM元素引用的属性和this在过滤器内指的DOM元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM