search

Home  >  Q&A  >  body text

javascript - Adjacent elements have spaces, how to find out the previous sibling element it is adjacent to, instead of #text?

<u>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</u>
aA=document.getElementsByTagName('li');

Take out the li element
Then bind the click event

for(var i = 0; i < aA.length; i++) {
    aA[i].onclick = function () {

Now I click on the second li element, but I want to find out the adjacent sibling elements of the second li element

Then I used .previousSibling; but what I found was #text, not the first li element.
When I deleted all the spaces in ul, what I found was the first element. element

Then now I want to find the previous sibling element of the li element without deleting spaces. Without using JQ

How to implement this function

ringa_leeringa_lee2762 days ago761

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-06-30 09:54:23

    previousElementSibling Go check it out

    另一种就是获取自己是第几个,然后parentElement然后children然后自己个数加减
    

    reply
    0
  • Cancelreply