私が書いているjQueryプラグインでは、プラグインが呼び出されているDOMオブジェクト内の特定の要素を確認したいと思います。コードは次のようになります。
//the object that you call the plugin on, stored in the variable "o"
o = this;
//Store children of a child element inside "o" in the variable "elInsideO"
elInsideO = o.find('selector').children('childrenSelector');
/* check if elInsideO is empty in case 'selector' is not present and then look for
alternative element 'selector2' */
if (elInsideO.length == 0) {
elInsideO = o.find('selector2').children('childrenSelector');
}
この選択を行うためのより効率的な方法はありますか?私が考えることができるもう1つの可能性はこれです:
if (o.find('selector').length != 0) {
elInsideO = o.find('selector').children('childrenSelector');
} else {
elInsideO = o.find('selector2').children('childrenSelector');
}
これらのソリューションのどれがより効率的ですか(つまり、パフォーマンスが向上します)?さらに良い別の方法はありますか?
助けてくれてありがとう!