admin管理员组文章数量:1244218
How can I get the text value from an elements's child?
Say I have this code on a page:
<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>
The function 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;
}
How would I go about getting that child's data: the <div class "text">
. How can I get that from the parent?
I'm still having problems. If there is two codeboxes on one page, then it does not work. Remember, I am unable to use ID's. It must be classes.
If I was able to use jQuery this would be easy.
How can I get the text value from an elements's child?
Say I have this code on a page:
<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>
The function 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;
}
How would I go about getting that child's data: the <div class "text">
. How can I get that from the parent?
I'm still having problems. If there is two codeboxes on one page, then it does not work. Remember, I am unable to use ID's. It must be classes.
If I was able to use jQuery this would be easy.
Share Improve this question edited Aug 29, 2011 at 2:00 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Jul 6, 2009 at 14:01 James BrooksJames Brooks 1,3115 gold badges18 silver badges28 bronze badges4 Answers
Reset to default 8Get a reference to the node you want to retrieve text from and try:
someNode.firstChild.nodeValue
When you have a node like this:
<span>Here is some text</span>
You're actually looking at two nodes, a span node which has a text node child. In DOM, that text node child's nodeValue is "Here is some text"
put an ID on the tag you want to get its data from.
this way will only grab the first child of the div node:
function copy(){
var text = document.getElementById( "YOU_TAG_NAME" ).firstChild;
//do stuff
}
this will grab all of the data in the node, but don't do this unless you have control over what goes into that div tag:
function copy(){
var text = document.getElementById( "YOU_TAG_NAME" ).innerHtml;
//do stuff
}
To clarify: Are you trying to get the css from the "text" class to display?
Just a thought, you could try using id
attributes to get what you need.
Try this:
Change your HTML slightly. The "javascript:" prefix isn't necessary inside an onclick handler. Also, pass a reference of "this" to your copy function:
<div class='geshitop'>
[ CODE ] [ <a href="#" onclick="copy(this); return false;">PLAIN-TEXT</a> ]
</div>
<div class='geshimain'>
<pre><div class="text" style="font-family:monospace;">Code goes here...</div></pre>
</div>
Having done that, alter your copy function to accept the new parameter. Then you just have to locate the correct node. From the question, I think you are looking for the next <div class="text"> that is a child of the <div class="geshimain"> that is the next sibling of the parent node that contains the link that was clicked. This function should acplish that:
function copy(node) {
node = node.parentNode; // <div class="geshitop">
// Loop over adjacent siblings, looking for the next geshimain.
while (node.nextSibling) {
node = node.nextSibling;
if (node.nodeName === 'DIV' && node.className === 'geshimain') {
break;
}
}
if (!node) {
throw new Error("Could not locate geshimain");
}
// Locate the <div class="text">
node = (function () {
var divs = node.getElementsByTagName('div');
for (var x = 0; x < divs.length; x++) {
if (divs[x].className === 'text') {
return divs[x];
}
}
return null;
}());
if (!node) {
throw new Error("Could not locate text");
}
node =
'<textarea name="code" cols="40" rows="15">' + node.innerHTML + "</textarea>";
popup = window.open("", "window", "resizeable,width=400,height=300");
popup.document.write(node);
popup.document.close();
}
Good luck!
本文标签: htmlJavascriptGet child39s text valueStack Overflow
版权声明:本文标题:html - Javascript - Get child's text value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740180265a2237204.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论