在我正在编写的 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');
}
有没有更有效的方法来进行这种选择?我能想到的另一种可能性是:
if (o.find('selector').length != 0) {
elInsideO = o.find('selector').children('childrenSelector');
} else {
elInsideO = o.find('selector2').children('childrenSelector');
}
这些解决方案中哪一个更有效(意味着性能更好)?还有其他更好的方法吗?
感谢您的帮助!