!代码: 判断

判断一个变量是否存在:   2016-3-30

if(typeof(GP)!=="undefined"){
    offerIndex.dropArea(GP,GC1);//省市下拉菜单初始化
}

判断一个元素是否存在:   2016-3-30

<script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    var s=$("#button").length;
    alert(s);  //为0就是不存在
});
</script>

js 中的in_array(判断某个元素是否在js数组中)

js 中的in_array(判断某个元素是否在js数组中)
Array.prototype.S = String.fromCharCode(2);  
Array.prototype.in_array = function(e) {  
    var r = new RegExp(this.S+e+this.S);  
    return (r.test(this.S+this.join(this.S)+this.S));  
}; 
 
var pre_arr = new Array();
pre_arr .in_array('test');//存在返回true 否则false

from: http://hi.baidu.com/bing2liuliu/item/7d9243628eb2712269105ba9

原文地址:https://www.cnblogs.com/qq21270/p/3472908.html