开发者

Insert variable as object in jQuery

I have a simple variable:

var spanId = 'hi';

Now, I want to generate this:

$('#hi')

using the variable.

So I tried:

$('#' + spanId)

But that did开发者_StackOverflow中文版n't work. Any suggestions?

Thanks, Amit


please note that the "+"-sign is a css selector, this might be a problem.

__________________ UPDATE __________________

I quickly checked it (See http://jsfiddle.net/r7MgY/1496/), it seems to work.

Code:

html (in body):

<div id="hi">test</div>​

css:

#hi {
    width: 100px;
    height: 100px;
    background: #ffb;
    padding: 10px;
    border: 2px solid #999;​
}

jquery:

$(function() {
    var spanId = "hi";
    alert($('#' + spanId).css('backgroundColor'));​
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜