所以我想简单地在jquery中输入数组。我想要实现的结果是:
$(".game-board, .building-tokens").click( function() {
$( ".mission-cards, .game-board, .building-costs, .mission-cards, .resources-cards, .resources-tokens, .road-card, .game-modal, .dice" ).toggleClass( "game-opacity" );
});
但这段代码太长了。并且必须有更合理的解决方案。所以我尝试将所有css类放在这样的数组中:
var classes = [".game-board", ".building-tokens", ".mission-cards", ".building-tokens", ".building-costs", ".mission-cards", ".resources-cards", ".resources-tokens", ".road-card", ".game-modal", ".dice"];
$(".game-board, .building-tokens").click( function() {
$('"' + classes.toString() + '"').toggleClass( "game-opacity" );
});
为什么这段代码不起作用?
答案 0 :(得分:1)
您可以使用数组的join()方法连接所有元素,然后
$(classes.join()).toggleClass("game-opacity")