您需要获得文本内容 or 内部文本特性:
var li = document.getElementById(id);
var t = "textContent" in li.childNodes[0] ? "textContent" : "innerText";
alert(li.childNodes[0][t]);
内部文本是由 IE 实现的文本内容由符合标准的浏览器实现。
或者,如果您确定该元素的第一个子节点将是文本节点,则可以使用
alert(li.childNodes[0].childNodes[0].nodeValue);
Based on your comment below, we can make a safe assumption that white-space is interfering with the
childNodes property in your example. This is normal -
childNodes returns both elements and text nodes that are direct descendants of the given element. The alternative is to use
children in newer browsers:
// 1st example using `children` instead
var li = document.getElementById(id);
var t = "textContent" in li ? "textContent" : "innerText";
alert(li.children[0][t]);
// 2nd example using `children` instead
alert(li.children[0].childNodes[0].nodeValue);
childrenFirefox 3.5 之前的版本和其他旧版浏览器不支持。你可以使用li.getElementsByTagName("a")对于您发布的示例 - 但请记住它将返回所有元素后代,而不仅仅是直系子元素。