开发者

create jquery selector from 2 variables

I would like to create (as an example) the following jQuery selector $('li').contents() from 2 variables (eg) variable1 = 'li' and variable2 = 'contents()'.

I know that variables can be concat开发者_开发知识库enated within the opening brackets eg $(var1 + var2) but I don't know how to add (eg) contents() to $('li').

TIA Paul


You should be able to call

var1 = 'li';
var2 = 'contents';
$(var1)[var2]();

Note that var2 needs to contain the function name without parentheses.

Of course, this only works if var2 always contains a function name, but you could go as far as to check if it contains parentheses and make a decision based on that facet. If var2 contains parentheses, call the function. If not, add it to var1 in the selector.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜