简体   繁体   English

获得孩子的价值<div>父母的<div>

[英]get value of child <div> of a parent <div>

<div id="parent">
    <div id="child">
        some-value
    </div>
</div>

how do I get "some-value"?我如何获得“一些价值”? I tried我试过

var parent = document.getElementById("parent");
var child = parent.childNodes[0];
var childval = child.value;
document.getElementById("output").innerHTML=childval;

it outputs "undefined".它输出“未定义”。

The value property only exists for form elements. value属性只存在于表单元素中。 If you want to get the content of any other elements, you can either use innerHTML [MDN] to get the content as HTML string, or textContent [MDN] resp.如果要获取任何其他元素的内容,可以使用innerHTML [MDN]以 HTML 字符串形式获取内容,也可以使用textContent [MDN]textContent [MDN]来获取内容。 innerText [MSDN] to only get the text content without HTML tags. innerText [MSDN]只获取没有 HTML 标签的文本内容。

childNodes [MDN] returns all child nodes, not only element nodes. childNodes [MDN]返回所有子节点,而不仅仅是元素节点。 That means, it also contains text nodes for example.这意味着,它还包含例如文本节点 The line break you have after <div id="parent"> is a text node as well. <div id="parent">之后的换行符也是一个文本节点。 Hence, parent.childNodes[0] returns the text node which consists only of a line break.因此, parent.childNodes[0]返回仅由换行符组成的文本节点。

If you want to get the first element node, you can either use children [MDN] (see browser compatibility ), or iterate over the child nodes, testing what kind of node each of them is.如果你想获得第一个元素节点,你可以使用children [MDN] (参见浏览器兼容性),或者迭代子节点,测试每个节点是什么类型的节点。 1 indicates an element node, 3 a text node: 1表示元素节点, 3表示文本节点:

var child = parent.firstChild;

while(child && child.nodeType !== 1) {
    child = child.nextSibling;
}

There are also other ways to retrieve elements, eg with getElementsByTagName [MDN] .还有其他方法可以检索元素,例如使用getElementsByTagName [MDN]

Or in your case, you can just use getElementById [MDN] to get a reference to both of the elements.或者在您的情况下,您可以使用getElementById [MDN]来获取对这两个元素的引用。

The problem is that parent <div> actuially has three children: a TextNode containing a new line after parent opening tag, the actual child <div> and yet another TextNode with newline after closing child tag.问题是parent <div>实际上有三个孩子:一个TextNodeparent开始标签后包含一个新行,实际的child <div>和另一个在关闭child标签后带有换行符的TextNode But hard-coding second item is a bad idea:但是硬编码第二项是一个坏主意:

var parent = document.getElementById("parent");
console.info(parent.childNodes.length);
var child = parent.childNodes[1];
var childval = child.innerHTML;

I would suggest iterating over children and finding the actual child or using我建议遍历孩子并找到实际的child或使用

parent.getElementsByTagName('div')

shorthand.速记。

That's one of the reasons why people love jQuery so much:这就是人们如此喜爱 jQuery 的原因之一:

$('#parent div').text()
var parent = document.getElementById("parent");
var child = parent.children[0];
var childval = child.innerHTML;
document.getElementById("output").innerHTML=childval;

DEMO : http://jsfiddle.net/bcqVC/2/演示: http : //jsfiddle.net/bcqVC/2/

To get all the <div> elements you can use:要获取所有<div>元素,您可以使用:

var div_val=prompt("Enter a div to Get the Child Elements","");
var div_ele=document.getElementById(div_val).childNodes;
for (var i=0, max=div_ele.length; i < max; i++) {
    alert(div_ele[i]); //All your Div Elements
}

document.getElementById("output").innerHTML = document.getElementById("child").innerHTML; document.getElementById("output").innerHTML = document.getElementById("child").innerHTML;

This will solve your problem.这将解决您的问题。

Using your way of approach try as shown below使用您的方法尝试如下所示

var parent = document.getElementById("parent");
var child = parent.childNodes[0];

var childval = child.innerHTML; var childval = child.innerHTML;

document.getElementById("outPut").innerHTML=childval;

This will also solve your problem这也将解决您的问题

try this way by this pointer.通过这个指针试试这种方式。

 var childs = document.getElementById('myDropdown').children; //returns a HTMLCollection for (var indx = 0; indx < childs.length; indx++) { // iterate over it childs[indx].onclick = function() { // attach event listener On Symbole Dive THIS . this.style.color = "#ff0000"; // add to note form the symbole . document.getElementById("Note_form").value += this.innerHTML; } }
 <div class="dropdown"> <div id="myDropdown" class="dropdown-content"> <div>♥</div> <div>☺</div> <div>π</div> <div>•</div> <div>Σ</div> <div>°</div> <div>Ω</div> <div>∞</div> <div>×</div> <div>÷</div> <div>≥</div> <div>≤</div> <div>≠</div> <div>®</div> <div>©</div> <div>¥</div> <div>£</div> <div>€</div> </div> </div> <textarea id="Note_form" class="copy_erea" placeholder="The Content.." oninput="note_Edite(this.value);"></textarea>

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

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