Which element does javascript get?

<div>
    <li class="img">1</li>
    <li class="img">2</li>
    <li class="img hover">3</li>
    <li class="img">4</li>
    <li class="img">5</li>
</div>

<span>3</span>

above your code hover will be automatically changed in 5 li. How can you rely on js to know which li is currently being hover without refreshing the page?

I thought of an idea, but I haven"t run it yet. In the test, I
first get how many li, there are. Then if the value of hover, in IndexOf.li is greater than 1, find out the subscript of li greater than 1
.
Mar.24,2021

after you get the element, you can know how many child elements it is through the parent element of the current element.
template

<ul>
  <li class="img">1</li>
  <li class="img">2</li>
  <li class="img hover">3</li>
  <li class="img">4</li>
  <li class="img">5</li>
</ul>
<button onclick="getHoverIndex()"></button>

script

function getHoverIndex() {
  var el = document.querySelector('.hover');
  var index = [].indexOf.call(el.parentElement.children, el);
  console.log(index)
}

event delegate to determine the value of the current clicked object e.target . Or you can bind an identifier attribute to the tag, class or attribute . I think so much for the time being. li is it appropriate to put it in ul ?


<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>- LI </title>
    <style>
    </style>
</head>
<body>
<ul>
    <li class="img">1</li>
    <li class="img">2</li>
    <li class="img">3</li>
    <li class="img">4</li>
    <li class="img">5</li>
</ul>
<script>
    var nodeList = document.getElementsByTagName('li');
    for (var i = 0; i < nodeList.length; iPP) {
        (function (j) {
            nodeList[j].addEventListener("click", function (e) {
                alert(j)
            }, false);
        })(i);
    }
</script>
</body>
</html>

binding this when distributing events can be solved either by self-calling function using closure or by using es6 syntax let

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ed67-41488.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ed67-41488.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?