what is the alternate function for indexOf for string in IE8?

£可爱£侵袭症+ 提交于 2020-01-03 15:56:12

问题


I have used indexOf to check whether i have particular text is present in a sentence or not like the following

var temp = "";
temp="data not available";
if(temp.indexOf("datas") == 0){
    alert("True");
}
else{
    alert("false");
}

The problem i was facing is indexOf is not supported in Internet Explorer 8. How to perform this operation in IE8 too? Is there any default alternate functions available in jquery?

I tried inArray but it supports only array.

fiddle


回答1:


I think you're confused about which indexOf() is supported.

You're using String.prototype.indexOf(), which is supported from IE6 onwards, according to Microsoft's documentation.

It's Array.prototype.indexOf() that's only supported from IE9 onwards.




回答2:


You can use String.search:

var temp = "data not available";
if(!temp.search("datas")){
    alert("True");
} else {
    alert("false");
}

Fiddle

(NB: I don't have IE8 to test it)



来源:https://stackoverflow.com/questions/21772308/what-is-the-alternate-function-for-indexof-for-string-in-ie8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!