简体   繁体   English

如何获取ul元素中不在js的li元素中的文本

[英]How to take text which is inside ul element not in li element of js

while I am taking inner text of ul element inner text of li and a tag also comes so how can I take exact inner text of ul element element only when I target it only. 虽然我正在获取ul元素的ul元素的内部文本,并且还带了标签,所以仅当我仅以ul元素为元素时,如何才能获取ul元素的确切内部文本。

my code:- 我的代码:

   <ul id="a3">
       hey
       <li id="a4">1 points again</li>
       <li id="a5">You have no points</li>
        <li id="a6"><a id="a7" href="cookie_catch.html">points review.<a>
        </li>
   </ul>

 <script>
   var el =  document.getElementById('a3'); 
   console.log(el.textContent)
 <script>

How can I take innerText of ul only 我如何只接受ul的innerText

You need to target the first child node of your element before looking up its text content, using .childNodes[..] : 您需要使用.childNodes[..]元素的第一个子节点,然后再查找其文本内容:

 var el = document.getElementById('a3'); console.log(el.childNodes[0].textContent) 
 <ul id="a3"> hey <li id="a4">1 points again</li> <li id="a5">You have no points</li> <li id="a6"><a id="a7" href="cookie_catch.html">points review.<a></li> </ul> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM