[英]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 属性,如
name
, id
, value
具有相应的属性中的DOM元素参考相匹配,因此这些值将会被复制到的DOM元素引用的属性和this
在过滤器内指的DOM元素
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.