jQuery示例:简单判断值是否存在于数组中的方法

2019-03-2209:51:51WEB前端开发Comments1,695 views字数 548阅读模式

本文实例讲述了jQuery简单判断值是否存在于数组中的方法。分享给大家供大家参考,具体如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10261.html

<!DOCTYPE>
<html>
<head>
<title>jquery判断值是否存在于数组中</title>
<meta charset="utf-8">
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
//参数第一个为数组,第二个为需要判断是否存在于数组中的值
function isInArray(arr,val){
var str = ","+arr.join(",")+",";
if(str.indexOf(","+val+",") != "-1"){
//该数存在于数组中
arr.splice($.inArray(val,arr),1); //把该数从数组中删除
console.log(arr);
}else{
//该数不存在于数组中
console.log("不存在");
}
}
var arr = [1,28,60,80,6];
isInArray(arr,28);//存在,删除元素28
isInArray(arr,18);//不存在,提示“不存在”
</script>
</body>
</html>文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10261.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10261.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/10261.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/10261.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定