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

StackOverflow https://stackoverflow.com/questions/21326399

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).

But the second console.log outputs undefined twice, instead of 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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top