0

I have this code which will hide everything inside a certain div:

$('#middlecolumn *').hide();

So now I would like to set the ID as a variable, so I did:

var qw = '#middlecolumn';

$(qw + '*').hide();

This doesn't seem to work. It hides the ID as well as everything inside it. How do I write it the proper way?

JsFiddle Example

4

4 回答 4

4

您需要使用空格字符:

var qw = '#middlecolumn';
$(qw + ' *').hide(); // will be equivalent to: "#middlecolumn *"

否则,插值会将其转换为: #middlecolumn*,这是无效的。

于 2013-04-14T10:35:44.510 回答
2

你错过了一个空间:

$(qw + ' *').hide();
于 2013-04-14T10:35:52.470 回答
2

您在选择器中缺少空格键。所以它会是

var qw = '#middlecolumn';

$(qw + ' *').hide(); // <<-- Spacebar before *
于 2013-04-14T10:36:05.240 回答
1

你缺少一个空格字符。

这个:

$(qw + '*').hide();

一定是:

$(qw + ' *').hide();
于 2013-04-14T10:39:00.553 回答