开发者

How do I extend jQuery's selector engine to warn me when a selector is not found?

开发者 https://www.devze.com 2023-02-28 10:24 出处:网络
Say I make a mistake when I\'m trying to find an element and I make a typo, like $(\'lsdkfj\').开发者_运维技巧

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?


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;
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号