本文介绍了为什么indexOf在Internet Explorer中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此函数在表单onSubmit期间执行,并且在Firefox和Chrome中运行正常,但在IE中则无法正常运行.我怀疑它是indexOf,但是我似乎找不到找到使它正常工作的方法.
This function executes during the forms onSubmit, and works fine in Firefox and Chrome, but not in IE. I suspect it's indexOf, but I cannot seem to find a way to get it to work.
function checkSuburbMatch(e) {
var theSuburb = document.getElementById('suburb').value;
var thePostcode = document.getElementById('postcode').value;
var arrayNeedle = theSuburb + " (" + thePostcode + ")";
if(suburbs.indexOf(arrayNeedle) != -1) {
alert("Suburb and Postcode match!");
return false;
} else {
alert("Suburb and Postcode do not match!");
return false;
}
}
推荐答案
IE在数组,但是您可以自己添加它, :
IE simply doesn't have this method on Array, you can add it yourself though, from MDC:
if (!Array.prototype.indexOf)
{
Array.prototype.indexOf = function(elt /*, from*/)
{
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}
如果缺少.indexOf()
,这会添加.indexOf()
(这时表示您在IE< 9中),然后就可以使用它了.至于为什么甚至IE8都没有呢?我不能在那帮你...
This adds .indexOf()
if it's missing (at this point that means you're in IE<9) then you can use it. As for why even IE8 doesn't have this already? I can't help you there...
这篇关于为什么indexOf在Internet Explorer中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!