Say I make a mistake when I'm trying to find an element and I make a typo, like $('lsdkfj')
.
Instead of jQuery returning me an empty array, I'd like to return an error message in the console, like
"The selector 'lsdkfj' cannot be found"
. What is the best way to go about doing this?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Like this:
var oldInit = $.fn.init;
$.fn.init = function(selector, context, rootjQuery) {
var result = new oldInit(selector, context, rootjQuery);
if (result.length === 0)
console.info("jQuery call has no elements!", arguments);
return result;
};