Home > Web Front-end > JS Tutorial > Expand some incompatible methods in IE such as contains, startWith, etc. _javascript skills

Expand some incompatible methods in IE such as contains, startWith, etc. _javascript skills

WBOY
Release: 2016-05-16 17:04:27
Original
1760 people have browsed it
复制代码 代码如下:

/**
* Extend startWith method
* @param str
* @return
*/
String.prototype.startWith=function(str){
if(str==null||str==""||this.length==0||str.length>this.length)
return false;
if(this.substr(0,str.length)==str)
return true;
else
return false;
return true;
};


/**
* Extend contains method
* @param item
* @return
*/
Array.prototype.contains = function(item){
return RegExp("\b" item "\b").test(this);
};


/**
* IE does not support the indexOf method, add the indexOf method for IE
*/
Array.prototype.indexOf = function(val){
var value = this;
for(var i =0; i < value.length; i ){
if(value[i] == val) return i;
}
return -1;
};
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template