忽略错误并继续在IE中运行javascript?
我的网页上有一个JIT Spacetree,IE不喜欢几行。如果我打开开发人员工具,并告诉它运行它们,它看起来很棒,并按它应该的方式加载所有内容 我有没有办法让它说“你知道吗,这些错误并不是真正的交易破坏者,让我们继续下去”?两个进一步缩进的行是违规者,以及jQuery1.6.4(将尝试使用1.7.1)中的$.getJSON或$.parseJSON忽略错误并继续在IE中运行javascript?,javascript,jquery,internet-explorer,Javascript,Jquery,Internet Explorer,我的网页上有一个JIT Spacetree,IE不喜欢几行。如果我打开开发人员工具,并告诉它运行它们,它看起来很棒,并按它应该的方式加载所有内容 我有没有办法让它说“你知道吗,这些错误并不是真正的交易破坏者,让我们继续下去”?两个进一步缩进的行是违规者,以及jQuery1.6.4(将尝试使用1.7.1)中的$.getJSON或$.parseJSON var style = label.style; style.width = node.data.offsetWidth;
var style = label.style;
style.width = node.data.offsetWidth;
style.height = node.data.offsetHeight;
style.cursor = 'pointer';
style.color = '#fff';
style.fontSize = '0.8em';
style.textAlign= 'center';
},
在try/catch中包装有问题的代码,不要在catch中执行任何操作。将有问题的代码包装在
try{}catch(e){}
块中,您应该可以开始了
下面这样的东西应该适合你
var style = label.style;
try {
style.width = node.data.offsetWidth;
style.height = node.data.offsetHeight;
} catch (e) {
//do alternate when error
}
style.cursor = 'pointer';
style.color = '#fff';
style.fontSize = '0.8em';
style.textAlign= 'center';
IE在定义对象时是“过敏”的,并在最后一个属性处留下逗号
坏的:
好:
var apple = { color : "yellow",
taste : "good" };
您可以使用try-catch语句
var style = label.style;
try
{
style.width = node.data.offsetWidth;
style.height = node.data.offsetHeight;
}
catch(err) { /* do nothing */ }
style.cursor = 'pointer';
style.color = '#fff';
style.fontSize = '0.8em';
style.textAlign= 'center';
您可以使用try…catch:
try{
allert('hello'); //Syntax error
}catch(err){
console.log(err);
}
如果您知道您的代码可能会遇到错误(无论出于何种原因),您可以通过try/catch捕捉并处理错误:
try {
// Code that is likely to error
} catch(e) {
// Handle the error here
}
您可以不处理错误,也可以尝试恢复应用程序。在这种情况下,您可能应该首先尝试找出IE抛出错误的原因,看看是否可以避免抑制错误
进一步阅读:
console.log
在旧版本9中未实现。
try {
// Code that is likely to error
} catch(e) {
// Handle the error here
}