如何在Javascript中获取父元素文本 [英] How to get the parent element text in Javascript
本文介绍了如何在Javascript中获取父元素文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在javascript中获取父元素中的文本?
How can i get the text in parent element in javascript?
<script language="javascript" type="text/javascript">
var elements= document.getElementsByTagName("span");
alert(elements[5].parent.innerText);
</script>
这是我正在使用的,但此代码未返回任何警报。
我可以使用什么属性来获取父标记中的文本?请帮助。
This is the what i am using, but this code is not returning any alerts.
What property can i use to get the text in parent tag? Please help.
推荐答案
尝试parentNode
而不是parent
JavaScript HTML DOM元素(节点) [ ^ ]
以下是一个常见的解决方法:找到要删除的子项,并使用其parentNode属性查找父项:
TryparentNode
instead ofparent
JavaScript HTML DOM Elements (Nodes)[^]
Here is a common workaround: Find the child you want to remove, and use its parentNode property to find the parent:
var child=document.getElementById("p1");
child.parentNode.removeChild(child);
为span指定一个id。
并试试这个
assign an id to span.
and try this
var a=document.getElementById("span_id");
alert(a.parentElement.innerText)
这篇关于如何在Javascript中获取父元素文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文