如何从元素的子元素中获取文本值?
假设我在页面上有这段代码:
<div class='geshitop'>
[ CODE ] [ <a href="#" onclick="javascript:copy(); return false;">PLAIN-TEXT</a> ]
</div>
<div class='geshimain'>
<pre><div class="text" style="font-family:monospace;">Code goes here...</div></pre>
</div>
函数copy()
:
<script type="text/javascript">
function copy() {
var text = this.parent.getElementsByName("text");
var code = text[0].value;
var popup = window.open("", "window", "resizeable,width=400,height=300");
popup.document.write("<textarea name='code' cols='40' rows='15'></textarea>");
popup.code.value = code;
}
我将如何获取那个 child 的数据:<div class "text">
.我怎样才能从 parent 那里得到它?
我还有问题。如果一页上有两个代码框,则它不起作用。请记住,我无法使用 ID。它必须是类。
如果我能够使用 jQuery,这会很容易。
最佳答案
获取对要从中检索文本的节点的引用并尝试:
someNode.firstChild.nodeValue
当你有这样一个节点时:
<span>Here is some text</span>
您实际上正在查看两个节点,一个具有文本节点子节点的跨度节点。在 DOM 中,该文本节点子节点的 nodeValue 是“Here is some text”
关于Javascript - 获取 child 的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1087142/