Home > Article > Web Front-end > The existence of functions and parameter issues in JavaScript’s jQuery library_jquery
jQuery function parameter passing
Using external variables in jQuery’s function:
//如何取得i的变量 for(i=0;i<3;i++) { $.get("/test.html", function(data){ alert(i) }); } //使用闭包: for(i=0;i<3;i++) { (function(index){ $.get("/test.html", function(data){ alert(index) }); })(i) }
JavaScript and jQuery detect whether a function exists
The method to detect whether a function exists in JavaScript is:
if(typeof $form.validate == 'function') { console.log('该 function 存在'); } else { console.log('该 function 不存在'); }
For example: Need to detect whether an MD5 method exists:
if(typeof window.md5 == 'function') { // 如果纯在该方法,才去调用 md5(286); }
To detect whether a jQuery method exists, you can use the following method:
if( jQuery.isFunction(jQuery.fn.rtFunction) ) { // 如果该方法存在,才去调用 jQuery(document).rtFunction(); }