私は別のクラスに同じ機能を適用したいと思います。このコードを短くする方法はありますか?jqueryコードを短くしてください
jQuery('.class1').after("<p>text</p>");
jQuery('.class2').after("<p>text</p>");
jQuery('.class3').after("<p>text</p>");
私は別のクラスに同じ機能を適用したいと思います。このコードを短くする方法はありますか?jqueryコードを短くしてください
jQuery('.class1').after("<p>text</p>");
jQuery('.class2').after("<p>text</p>");
jQuery('.class3').after("<p>text</p>");
あなたは、単一の 結果に結合するセレクタの任意の数を指定することができMultiple Selector (“selector1, selector2, selectorN”)
jQuery('.class1, .class2, .class3').after("<p>text</p>");
を使用することができます。この多重表現コンビネータは、異種要素を選択する効率的な方法です。 返されたjQueryオブジェクト内のDOM要素の順序は、ドキュメントの順序 にあるため、一致しないことがあります。このコンビネータの代替方法は、.add() メソッドです。
for(var i = 1; i <= 3; i++)
jQuery('.class' + i).after("<p>text</p>");
か:
jQuery('.class1,.class2,.class3').after("<p>text</p>");
次のようなattribute starts with selectorを使用することができます。
jQuery('[class^="class"]').after("<p>text</p>");