How do I extend jQuery's selector engine to wa

2019-02-19 05:15发布

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?

标签: jquery sizzle
1条回答
小情绪 Triste *
2楼-- · 2019-02-19 06:00

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;
};
查看更多
登录 后发表回答