Я перебираю массив функции hasTile (tilesArray) {для (var i = 0; i <tilesArray.length; i ++) {console.log (tilesArray [t]); if (tilesArray [i] .firstChild) {console.log («Ребенок существует!»); } else {console.log («Нет ребенка»); }}}, и мне нужно проверить, есть ли в каждом элементе массива дочерний элемент.
Запуск кода ниже будет <div class = "container" id = "tileContainer"> <! - id, указанным в виде столбца строки. -> <div class = "row"> <div class = "col-xs-6 col-md-12 tile" id = "r1c1" onclick = "main (this)"> </ div> <div class = "col-xs-6 col-md-12 tile" id = "r1c2" onclick = "main (this)"> </ div> <div class = "col-xs-6 col-md-12 tile" id = "r1c3" onclick = "main (this)"> </ div> </ div> <div class = "row"> <div class = "col-xs-6 col-md-12 tile" id = "r2c1" onclick = "main (this)"> </ div> <div class = "col-xs-6 col-md-12 tile" id = "r2c2" onclick = "main (this)"> </ div> <div class = "col-xs-6 col-md-12 tile" id = "r2c3" onclick = "main (this)"> </ div> </ div> <div class = "row"> <div class = " col-xs-6 col-md-12 tile "id =" r3c1 "onclick =" main (this) "> </ div> <div class =" col-xs-6 col-md-12 tile "id =" r3c2 "onclick =" main (this) "> </ div> <div class =" col-xs-6 col-md-12 tile "id =" r3c3 "onclick =" main (this) "> </ div> </ div> </ div> <! - Конец контейнера для плитки -> <div class = "container" id = "turnDisplayContainer"> <div class = "row"> <div class = "col-xs-9 col-md-6 content "id =" userTurnDisplay "> <h4> Ваш поворот </ h4> </ div> <div class =" col-xs-9 col-md-6 content "id =" computerTu rnDisplay "> <h5> Поворот компьютера </ h5> <iframe src =" assets / media / load-wheel.gif "width =" 50 "height =" 50 "scrolling =" no "frameBorder =" 0 "> </ iframe> </ div> </ div> </ div> <! - <div class = "container" id = "timerDisplayContainer"> <div class = "row"> <div class = "col-xs-18 col -md-12 content "id =" timerDisplay "> <h5> Timer </ h5> </ div> </ div> </ div> -> <div class =" container btnContainer "> <div class =" row "> <button type =" button "class =" btn btn-danger "id =" resetButton "> Сброс </ button> </ div> </ div> <img class =" img img-responsive gameIcon "src =" активы / img / green-ring.png "alt =" Значок зеленого кольца "id =" userIcon "/> <img class =" img img-responsive gameIcon "src =" assets / img / red-x.png "alt = «Red X Icon» id = «computerIcon» /> для каждого элемента, однако я хочу использовать hasTile (tilesArray) {var bAllHaveChild = tilesArray.length> 0; for (var i = 0; i <tilesArray.length; i ++) {console.log (tilesArray [t]); if (! tilesArray [i] .firstChild) {bAllHaveChild = false; }} if (bAllHaveChild) console.log («Ребенок существует!»); else console.log («Нет ребенка»); } «ребенок существует!» при условии, чтокаждая плитка в элементе tilesArray имеет дочерний элемент. Как это можно достичь?
document.evaluate
index.html
const xpath = 'boolean(.//div/div/text())'
const hasTileXPath = (id) => document.evaluate(xpath, document.getElementById(id)).booleanValue
const hasTile = (id) => Array.from(
document.querySelectorAll(`#${id} > div > div`)
).filter((x) => x.innerHTML).length > 0
const iterations = 1000
const iterable = Array(iterations).fill().map((x, i) => i)
const pre = performance.now()
for(let i of iterable) {
hasTileXPath('hasNoTiles')
hasTileXPath('hasTiles')
}
const mid = performance.now()
for(let i of iterable) {
hasTile('hasNoTiles')
hasTile('hasTiles')
}
const last = performance.now()
console.info(`XPath: ${iterations} iterations @ ${(mid - pre) / 1000}s`)
console.info(`SHOULD NOT HAVE TILES (XPATH) => ${hasTile('hasNoTiles')}`)
console.info(`SHOULD HAVE TILES (XPATH) => ${hasTile('hasTiles')}`)
console.info(`querySelectorAll: ${iterations} iterations @ ${(last - mid) / 1000}s`)
console.info(`SHOULD NOT HAVE TILES => ${hasTileXPath('hasNoTiles')}`)
console.info(`SHOULD HAVE TILES => ${hasTileXPath('hasTiles')}`)
javascript,html,function,loops,