我有很長的引數串列,我必須傳遞給 jQuery.removeClass函式。
如何將它們全部添加到一個以逗號分隔的括號中?目前看起來是這樣的......
$("#bottom-nfl").click(function(){
$("#bottom-mlb i, #bottom-mlb small").removeClass("orange");
$("#bottom-nba i, #bottom-nba small").removeClass("orange");
$("#bottom-ncaab i, #bottom-ncaab small").removeClass("orange");
$("#bottom-profile i, #bottom-profile small").removeClass("orange");
});
但我希望它看起來像這樣:
$("#bottom-nfl").click(function(){
$("#bottom-mlb i, #bottom-mlb small,
#bottom-nba i, #bottom-nba small,
#bottom-ncaab i, #bottom-ncaab small
#bottom-profile i, #bottom-profile small").removeClass("orange");
});
但這會導致unterminated string literal錯誤。
我嘗試使用反斜杠來逃避換行符,但這也不起作用
uj5u.com熱心網友回復:
如果要拆分字串并允許您格式化串列,則此處需要模板文字
你也錯過了一個逗號
$("#bottom-nfl").on("click", function(){
$(`#bottom-mlb i, #bottom-mlb small,
#bottom-nba i, #bottom-nba small,
#bottom-ncaab i, #bottom-ncaab small,
#bottom-profile i, #bottom-profile small`).removeClass("orange");
});
或編程
$("#bottom-nfl").on("click", function(){
["mlb","nba","ncaab","ncaab","profile"]
.forEach(cls => $(`#bottom-${cls} i, #bottom-${cls} small`).removeClass("orange"));
});
實際上轉義換行符應該有效,但模板文字更漂亮
$("#bottom-nfl").on("click", function(){
$("#bottom-mlb i, #bottom-mlb small,\
#bottom-nba i, #bottom-nba small,\
#bottom-ncaab i, #bottom-ncaab small,\
#bottom-profile i, #bottom-profile small").removeClass("orange");
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/338595.html
標籤:javascript 查询
上一篇:按順序切換類
