How can I get JQuery to report an error-, jquery to report an error?
0. resolved issues
A. When the selector syntax is correct and no element is found, let jquery report an error.
B. When there is a problem with the selector syntax and the program cannot continue execution, let jquery report an error
It is mainly used to pass strings. Back up the jquery library before trying. It is best to change the name and add the name to be differentiated.
1. Environment Configuration
Jquery-3.0.0.js unzipped version
2. There is no problem with the selector syntax and no element can be found.
Add code to modify jquery source code. The following is the modified Code. The added code is highlighted in red.
Var version = "3.0.0", // Define a local copy of jQuery = function (selector, context) {// The jQuery object is actually just the init constructor 'enabled' // Need init if jQuery is called (just allow error to be thrown if not supported DED) if (typeof window ["[]"] = "undefined") {window ["[]"] = []; window ["[]"] ["selector"] = selector; window ["[]"] ["current"] = 0 ;} else {window ["[]"] ["current"] ++;} var c = new jQuery. fn. init (selector, context) if (window ["[]"] ["current"] -- = 0) {if (c. length = 0) {window. console. error (window ["[]"] ["selector"] + "cannot be found");} delete window ["[]"];} return c ;},
3. If the selector syntax is incorrect, the program cannot continue execution.
Code example
$ (Function ()
{
Var lol = $ ("li :");
Alert (lol. length );
});
Add code to the jquery source code based on the code added in the previous question. The following is the modified Code, and red is the added part.
Process = special? MightThrow: function () {try {mightThrow ();} catch (e) {if (jQuery. deferred. exceptionHook) {jQuery. deferred. exceptionHook (e, process. stackTrace);} // Support: Promises/A + section 2.3.3.3.4.1 // https://promisesaplus.com/#point-61 // Ignore post-resolution tions if (depth + 1> = maxDepth) {// Only substitute handlers pass on context // and multiple values (non-spec behavior) if (handler! = Thrower) {that = undefined; args = [e];} deferred. rejectWith (that, args);} if (typeof window ["[]"]! = "Undefined") {if (window ["[]"] ["current"] = 0) {window. console. error (window ["[]"] ["selector"] + "Preventing program execution from going down ");}}}};
As for the reason why the program cannot be executed downward, I cannot give a positive answer. JavaScript function calls always return content. When I use the debug tool to find out, the end of this function does not go. How can we say it? Come on!
Found the causeVar lol = $ ("li:"); an error is reported, resulting in program termination
Try
{
var lol = $("li:");
}
catch(e)
{
alert(e);
}