Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
184 views
in Technique[技术] by (71.8m points)

html - Using Javascript to find the tabindex of the last entry of a list and then extract innerText from it

I would like to use Javascript to extract innerText from the last entry of an html list on a web page. The list uses tabindex, so that seems like the best way of identifying the last entry. However, the list is also dynamic, so the tabindex number of the final entry will change day to day. I'm pretty new at this, but it would seem that I need the script to find the tabindex number of the last entry, and then use document.querySelector to get the innerText of that entry? Here's an example of what the list looks like:

<div id="container">
  <ul id=“list”>
    <li class=“entry” tabindex=“0” id=“entry0”></li>
    <li class=“entry” tabindex=“1” id=“entry1”></li>
    <li class=“entry” tabindex=“2” id=“entry2”></li>
    <li class=“entry” tabindex=“3” id=“entry3”></li>
    <li class=“entry” tabindex=“4” id=“entry4”></li>
  </ul>
</div>

I've also been reviewing some similar questions with great answers, but I've had difficulty modifying the code to do exactly what I'm looking for. This is the closest solution I've found on the site(answer provided by Elliot Frisch).

function moveNext(tabInput) {
  var lastTab = 3;
  var curIndex = tabInput.tabIndex;
  var tabs = document.getElementsByTagName("input");
  if(curIndex >= lastTab) { //if we are on the last tab then go back to the beginning
    curIndex = 0;
  }
  for(var i=0; i < tabs.length; i++) { // loop over the tabs.
    if(tabs[i].tabIndex == (curIndex+1)) { // is this our tab?
        tabbables[i].focus(); // Focus and leave.
        break;
    }
}

Does anybody have any ideas? I'd love to crack this nut.

Thanks for any assistance.

question from:https://stackoverflow.com/questions/66055187/using-javascript-to-find-the-tabindex-of-the-last-entry-of-a-list-and-then-extra

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...