Javascript 如何在jquery插件中添加try-catch块
我有一个jQuery插件。我想在我的jQuery插件中添加用于异常处理的try-catch块 我的插件Javascript 如何在jquery插件中添加try-catch块,javascript,jquery,jquery-plugins,Javascript,Jquery,Jquery Plugins,我有一个jQuery插件。我想在我的jQuery插件中添加用于异常处理的try-catch块 我的插件 $(document).ready(function(){ $('.requiredclass').keyup(function() { $(this).pluginMethod(); }); }); (function($) { //i Want try catch block for this part // jQuery plugin definition $.fn.plu
$(document).ready(function(){
$('.requiredclass').keyup(function() {
$(this).pluginMethod();
});
});
(function($) { //i Want try catch block for this part
// jQuery plugin definition
$.fn.pluginMethod = function(e) {
return this.each(function() {
var $this = $.this;
var som= $this.val();
//My Code goes here
});
};
})(jQuery);
现在,如果我想添加try-catch块,那么插件会是什么样子?
对于jquery函数,我们可以这样做
功能
function myFunction() {
//varible declarations
try {
//Code goes here
}
catch(err) { //We can also throw from try block and catch it here
alert("err");
}
finally {
//code for finally block
}
}
现在这是我们在函数中知道的格式。但是如果我想在插件中添加异常处理,该格式是什么呢?在来自(函数($){
的插件中,插件启动,然后有$.fn.pluginMethod=函数(e){
,后面是
return this.each(function() {`. So where to start the try block and stop it,where to put catch block.Can any one suggest me the format.
如果有人对这个问题的理解有任何疑问,请让我知道,我将尝试更详细地解释。像这样尝试
try{
(function($) {
// jQuery plugin definition
$.fn.pluginMethod = function(e) {
return this.each(function() {
var $this = $.this;
var som= $this.val();
//My Code goes here
})(jQuery);
}
catch(error)
{
alert(error);
}
我想我真的不明白你的问题。你需要更清楚一点 但这是你想要的吗
$(document).ready(function(){
$('.requiredclass').keyup(function() {
$(this).pluginMethod();
});
});
try {
(function($) {
//jQuery plugin definition
$.fn.pluginMethod = function(e) {
return this.each(function() {
var $this = $.this;
var som= $this.val();
//your Code goes here
});
};
})(jQuery);
} catch(err) { //We can also throw from try block and catch it here
alert("err");
} finally {
//code for finally block
}
在这个(函数($){I have})(jQuery)的末尾;所以我需要在}之后完成try块(jQuery);。
try {
//Block of code to try
}
catch(err) {
// Block of code to handle errors
}