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

巧了我就是萌 提交于 2019-12-01 10:08:35

问题


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


来源:https://stackoverflow.com/questions/5744997/how-do-i-extend-jquerys-selector-engine-to-warn-me-when-a-selector-is-not-found

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!