Как различить два тэга p по позиции в которой они появляются на странице?
Есть два тэга p которые идут в последовательности, нужно сделать проверку на правильность этой последовальности. Сделал проверку по textContent, но при не правильной последовательности всё равно проверка срабатывает успешно т.к. ей не важно в какой они последовальности
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
const hello = '<p id="idHello">hello</p>'
const world = '<p id="idWorld">world</p>'
const button = '<button id="idButton" onclick="showanything()"> BUTTON </button>'
var click = function click() {
document.getElementById('idButton').click()
}
var showanything = function showanything() {
alert('Вы нажали на кнопку')
}
const promise = new Promise(function(resolve, reject) {
setTimeout(() => {
document.write(hello)
resolve(setTimeout(() => {
document.write(world)
}, 2000))
}, 700)
})
promise.then(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
document.write(button)
resolve()
}, 3000)
}).then(click)
})
</script>
<script>
let checkTimer = setTimeout(function tick() {
// i ++
// console.log(i)
const p = new Promise(function(resolve, reject) {
resolve(idHello, idWorld)
})
.then(function(idHello) {
if (idHello.textContent == 'hello') {
console.log('hello')
}
return idWorld
})
.catch(function(error) {
console.log('Ошибка')
})
.then(function(idWorld) {
if (idWorld.textContent == 'world') {
console.log('world')
console.log('нашел параграф')
clearTimeout(checkTimer)
}
})
checkTimer = setTimeout(tick, 1000)
}, 1000)
</script>
</body>
</html>
Ответы (1 шт):
Автор решения: Инквизитор
→ Ссылка
document.getElementsByTagName('p'), найти <p> с нужными вам айдишниками, сравнить индексы. Чей индекс больше, тот и позже.