为什么我不能在$ .each函数中使用`this`作为jQuery函数的字符串arg

时间:2021-02-28 06:52:58

I'm struggling with some jQuery code I'm working on, and I've narrowed down the problem to this simple example that doesn't behave as I would expect it too:

我正在努力处理我正在研究的一些jQuery代码,并且我已经将问题缩小到这个简单的例子,它不像我期望的那样:

jsFiddle: http://jsfiddle.net/HypYT/

jsFiddle:http://jsfiddle.net/HypYT/


HTML

HTML

<div id="a">a: <span>A</span></div>
<div id="b">b: <span>B</span></div>​

jQuery

jQuery的

jQuery.each( ["#a", "#b"], function(){
    alert(this);    // alerts "#a" and "#b"
    $(this).hide(); //not working
    $('#b').hide(); //working
});​

Anyone have any insight what's going on here?

任何人都有任何见解这里发生了什么?

4 个解决方案

#1


6  

If a function is called on a string (or other primitive), this becomes a boxed object.
typeof this will therefore return "object", not "string".
This breaks jQuery. (no pun intended)

如果在字符串(或其他基元)上调用函数,则该函数将成为盒装对象。因此,typeof将返回“object”,而不是“string”。这打破了jQuery。 (没有双关语)

You can fix this by forcing it back to a string primitive:

你可以通过强制它回到字符串原语来解决这个问题:

$(String(this)).hide()

You can fix this for arbitrary primitives by writing

您可以通过编写为任意基元修复此问题

var self = this.constructor(this);

However, using the second argument passed to .each() will be faster, because it will avoid boxing in the first place.

但是,使用传递给.each()的第二个参数会更快,因为它将首先避免装箱。

#2


6  

Adding to SLaks answer, You can use the arg passed to .each. See below,

添加到SLAK答案,您可以使用传递给.each的arg。见下文,

jQuery.each( ["#a", "#b"], function(idx, el){
    alert(this);    // alerts "#a" and "#b"
    $(el).hide(); 
    //$('#b').hide(); 
});​

or You can do

或者你可以做到

var elToHide = $(["#a", "#b"].join()).hide();
$(elToHide.join()).hide()

DEMO: http://jsfiddle.net/HypYT/2/

演示:http://jsfiddle.net/HypYT/2/

#3


1  

You just need to do:

你只需要这样做:

$('#a, #b').hide();

#4


1  

the each function works in this way (http://api.jquery.com/jQuery.each/)

每个函数都以这种方式工作(http://api.jquery.com/jQuery.each/)

$.each([52, 97], function(index, value) { 
  alert(index + ': ' + value); 
});

so in your case

所以在你的情况下

jQuery.each( ["#a", "#b"], function(index, value){
    alert(value);    // alerts "#a" and "#b"
    $(value).hide(); //not working
});

​ The explanation is (taken from the link) that "The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value."

解释是(取自链接)“该值也可以通过this关键字访问,但Javascript将始终将此值包装为Object,即使它是一个简单的字符串或数字值。”

#1


6  

If a function is called on a string (or other primitive), this becomes a boxed object.
typeof this will therefore return "object", not "string".
This breaks jQuery. (no pun intended)

如果在字符串(或其他基元)上调用函数,则该函数将成为盒装对象。因此,typeof将返回“object”,而不是“string”。这打破了jQuery。 (没有双关语)

You can fix this by forcing it back to a string primitive:

你可以通过强制它回到字符串原语来解决这个问题:

$(String(this)).hide()

You can fix this for arbitrary primitives by writing

您可以通过编写为任意基元修复此问题

var self = this.constructor(this);

However, using the second argument passed to .each() will be faster, because it will avoid boxing in the first place.

但是,使用传递给.each()的第二个参数会更快,因为它将首先避免装箱。

#2


6  

Adding to SLaks answer, You can use the arg passed to .each. See below,

添加到SLAK答案,您可以使用传递给.each的arg。见下文,

jQuery.each( ["#a", "#b"], function(idx, el){
    alert(this);    // alerts "#a" and "#b"
    $(el).hide(); 
    //$('#b').hide(); 
});​

or You can do

或者你可以做到

var elToHide = $(["#a", "#b"].join()).hide();
$(elToHide.join()).hide()

DEMO: http://jsfiddle.net/HypYT/2/

演示:http://jsfiddle.net/HypYT/2/

#3


1  

You just need to do:

你只需要这样做:

$('#a, #b').hide();

#4


1  

the each function works in this way (http://api.jquery.com/jQuery.each/)

每个函数都以这种方式工作(http://api.jquery.com/jQuery.each/)

$.each([52, 97], function(index, value) { 
  alert(index + ': ' + value); 
});

so in your case

所以在你的情况下

jQuery.each( ["#a", "#b"], function(index, value){
    alert(value);    // alerts "#a" and "#b"
    $(value).hide(); //not working
});

​ The explanation is (taken from the link) that "The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value."

解释是(取自链接)“该值也可以通过this关键字访问,但Javascript将始终将此值包装为Object,即使它是一个简单的字符串或数字值。”