最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js的indexOf方法使用

 更新時間:2023年05月17日 15:52:57   作者:Smile_zxx  
indexOf() 方法可返回數(shù)組中某個指定的元素位置,本文就來介紹一下js的indexOf方法使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下

一、數(shù)組調用

  • indexOf() 方法可返回數(shù)組中某個指定的元素位置。
  • 該方法將從頭到尾地檢索數(shù)組,看它是否含有對應的元素。開始檢索的位置在數(shù)組 start 處或數(shù)組的開頭(沒有指定 start參數(shù)時)。如果找到一個 item,則返回 item 的第一次出現(xiàn)的位置。
  • 如果在數(shù)組中沒找到指定元素則返回 -1。
//語法
// array.indexOf(item,start) 
//item 必須 要查找的元素的位置,
//start 非必須可選的整數(shù)參數(shù)。規(guī)定在數(shù)組中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該參數(shù),則將從字符串的首字符開始檢索。
let food= ["番茄", "胡蘿卜", "排骨", "蘋果"];
let a = food.indexOf("蘋果");
console.log(a) // 3
let b= food.indexOf("香蕉");
console.log(b) // -1

二、字符串調用

  • indexOf() 方法可返回某個指定的字符串值在字符串中首次出現(xiàn)的位置。
  • 區(qū)分大小寫
  • 如果要檢索的字符串值沒有出現(xiàn),則該方法返回 -1。
//語法
//string.indexOf(value,start)
// value  必須 要查找的元素的位置
// start 可選的整數(shù)參數(shù)。規(guī)定在字符串中開始檢索的位置。它的合法取值是 0 到 string.length - 1。如省略該參數(shù),則將從字符串的首字符開始檢索。
let str="Hello world!";
console.log(str.indexOf("Hello"));//0
console.log(str.indexOf("World") );//-1
console.log(str.indexOf("world"));//6

三、應用例子

可以實現(xiàn)多項選擇

<template>
?? ?<div class="biaoqian">
?? ??? ??? ??? ?<button v-for="(item,index) in biaoqianList"?
?? ??? ??? ??? ?:key='index'?
?? ??? ??? ??? ?class="btn"?
?? ??? ??? ??? ?type="default"?
?? ??? ??? ??? ?size="mini"
?? ??? ??? ??? ?:class="{'active': isChange.indexOf(index)!=-1}"?
?? ??? ??? ??? ?@click="clickBtn(index)">{{item}}</button>
?? ??? ?</div>
</template>
export default{
?? ??? ?data(){
?? ??? ??? ?return{
?? ??? ??? ? ? ?isChange:[], ?//多選
?? ??? ??? ??? ?biaoqianList:['早餐','午餐','晚餐','宵夜'],
?? ??? ??? ??? ?foodChose:[]
?? ??? ??? ? ?}
?? ??? ??? ?},
?? ??? ?methods:{
?? ??? ??? ??? ?clickBtn(index){
?? ??? ??? ??? ??? ?// 多選
?? ??? ??? ??? ??? ?if (this.isChange.indexOf(index) == -1) {
?? ??? ??? ??? ??? ??? ?if(this.isChange.length == 4){
?? ??? ??? ??? ??? ??? ??? ?uni.showToast({
?? ??? ??? ??? ??? ??? ??? ??? ?title:'最多選擇四項',
?? ??? ??? ??? ??? ??? ??? ??? ?icon:'none'
?? ??? ??? ??? ??? ??? ??? ?})
?? ??? ??? ??? ??? ??? ?}else{
?? ??? ??? ??? ??? ??? ??? ?this.isChange.push(index);//選中添加到數(shù)組里
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ??? ?this.isChange.splice(this.isChange.indexOf(index), 1); //取消選中
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?console.log(this.isChange)
?? ??? ??? ??? ??? ?// let biaoqianList = ?[]
?? ??? ??? ??? ??? ?// for(let index in this.isChange){ //biaoqianList里面的索引重新加入
?? ??? ??? ??? ??? ?// ?? ?biaoqianList.push(this.biaoqianList[this.isChange[index]])
?? ??? ??? ??? ??? ?// }
?? ??? ??? ??? ?},
?? ??? ?}
}
<style lang="less">
?? ?.biaoqian{
?? ??? ??? ??? ?display: flex;
?? ??? ??? ??? ?justify-content: start;
?? ??? ??? ??? ?align-items: center;
?? ??? ??? ??? ?.active{
?? ??? ??? ??? ??? ?background-color: #76d2f4 ;
?? ??? ??? ??? ??? ?color: white;?? ?
?? ??? ??? ??? ?}
?? ??? ??? ??? ?.btn{
?? ??? ??? ??? ?border:0.01px solid #76d2f4;
?? ??? ??? ??? ?background-color:white ;
?? ??? ??? ??? ?color: #76d2f4;
?? ??? ??? ?}
?? ??? ?}
</style>

到此這篇關于js的indexOf方法使用的文章就介紹到這了,更多相關js indexOf內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

丽江市| 盐源县| 平武县| 盐山县| 浏阳市| 阳曲县| 运城市| 额济纳旗| 宣威市| 清涧县| 昔阳县| 藁城市| 中卫市| 泰顺县| 阳城县| 白银市| 华亭县| 古交市| 彝良县| 连云港市| 林甸县| 油尖旺区| 仲巴县| 泗水县| 汉寿县| 清远市| 都江堰市| 太仆寺旗| 门头沟区| 昂仁县| 清流县| 高阳县| 沙坪坝区| 二连浩特市| 自贡市| 长阳| 揭东县| 仁怀市| 嫩江县| 汤阴县| 常山县|