鍍金池/ 問答/HTML5  PHP  HTML/ js數(shù)組成員為對象時,如何找字符串在數(shù)組中的位置?

js數(shù)組成員為對象時,如何找字符串在數(shù)組中的位置?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
        //因為是對象數(shù)組所以查詢失敗
        function test1() {
            var divs = document.querySelectorAll("div");
            arr = Array.prototype.slice.call(divs);

            var locationArr = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].indexOf('li') >= 0) {
                    locationArr.push(i);
                }
            }
            console.log(arr);
            console.log(locationArr); //字符串在數(shù)組中的位置。     
            
        }
        
        //下面是查找字符串在數(shù)組中的位置因為數(shù)組文本是內(nèi)容所以查詢成功
        function test2() {
            var arr = ['zhao', 'qian', 'sun', 'li', 'zhou'];
            var locationArr = [];
            for (var i = 0; i < arr.length; i++) {
                if (arr[i].indexOf('li') >= 0) {
                    locationArr.push(i);
                }
            }
            console.log(arr);
            console.log(locationArr); //字符串在數(shù)組中的位置。         
        }
        
        
    
    </script>
</head>
<body>

<button onclick="test1()">查找對象數(shù)組字符串</button>
<button onclick="test2()">查找文本數(shù)組字符串</button>

<div id="rongqi">
    <div>zhao</div>
    <div>qian</div>
    <div>sun</div>
    <div>li</div>
    <div>zhou</div>    
</div>
</body>
</html>
回答
編輯回答
厭遇

tagName localName nodeName
無非就是比較這個東西嘛。。。

2017年4月5日 08:35
編輯回答
逗婦惱

其實可以把你的詳細(xì)需求重新說一下,看代碼的話基本沒有這樣去使用的。你應(yīng)該糾結(jié)的是編碼方向的問題,感覺你這個思路不正確

2017年12月14日 10:37