我怎样才能获得“id”?共 44<输入类型 =“文本”>输入类型>
这应该很容易,但是在研究了 DOM 并仔细阅读了 SO 上的相关帖子之后,我仍然不在那里。
我有一个最外面的 div,其中嵌套了 4 个 div。 4 个嵌套 div 中的每一个都有一个嵌套在其中的输入 type="text"。
我在最外面的包含 div 中添加了一个 onclick,以便单击 4 个嵌套 div 或 input type="text" 元素中的任何一个都会调用一个“onclick”处理程序。
在 onclick 处理程序中,我还需要获取 4 个文本输入和 4 个嵌套 div 的 id、名称和值。
这是代码。
<div class="itemlistRowContainer" id="topmostDiv ID" onclick="handleRowClick(this);"
name="itemlistRowContainer Div name" value="itemlistRowContainer Div Value">
<div class="itemListRowElementLeftMost" name="itemListRowElementLeftMost Div name"
value="itemListRowElementLeftMost Div value">Item<br />
<input type="text" size="30" maxlength="50" id="itemName_id"
name="itemName" value="$itemname" readonly="readonly"></input>
</div>
// 3 more divs and nested text input pairs not shown for brevity
</div>
这是 onclick 事件处理程序 - 请注意上面在最外面的 div 中,我将“this”传递给 onclick() 处理程序,从我读到的各种 SO 帖子中,我相信“this”将最外面的 div 本身传递给 onclick 处理程序:
function handleRowClick(theElement)
{
var childNodes = theElement.childNodes;
var numChildNodes = childNodes.length;
alert("handleRowClick: childNodes array has length = " + numChildNodes);
for(i = 0; i < numChildNodes; i++)
{
alert("The DOM element " + i + " has innerHTML = " + childNodes[i].innerHTML + ", "
+ "nodeName = " + childNodes[i].nodeName + ", "
+ "nodeType = " + childNodes[i].nodeType + ", "
+ "nodeValue = " + childNodes[i].nodeValue + ", "
+ "parentNode = " + childNodes[i].parentNode + ", "
+ "childNodes = " + childNodes[i].childNodes + ", "
+ "attributes = " + childNodes[i].attributes + ", "
+ "name = " + childNodes[i].name + ", "
+ "value = " + childNodes[i].value + ", "
+ "id = " + childNodes[i].id);
}
上面的第一个alert()显示childNodes数组中有9个数组元素。
当“for”循环执行时,它告诉我数组中有 2 种类型的 DOM 元素:
有 5 种报告为:nodeName=#text、nodeType = 3、 和 value= id=name=undefined
并且有 4 个 childNode[] 元素报告为:nodeName=DIV,nodeType = 1, 和 AGAIN - value=name=id=undefined。
在我的意见 - 看起来在 onclick 处理程序中,我正确地传递了 4 个嵌套 div (nodeType 1) 和 4 个只读文本字段,nodeType=#text,nodeType=3,输入类型=text。
然后“for”循环报告的第 9 个也是最后一个元素是——一个额外的 nodeName=#text 元素(nodeType = 3),我不知道那是什么——如果它是最外层的 div 会像 4 个内部 div 一样具有 nodeType=1 和 nodeName=div 。
很好,花花公子。但到底为什么 id、名称和值无法帮助我找出哪个是哪个?
DIV 节点的innerHTML 具有嵌套在其中的输入type=text 的html 标签,但我不打算解析它——DIV 的输出(在上面“for”循环的alert() 框中)节点是这样的:
The DOM element 1 has innerHTML = Item<br>
<input size="30" maxlength="50" id="itemName_id" name="itemName"
value="f's first item" readonly="readonly"
type="text">
, nodeName = DIV, nodeType = 1, nodeValue = null,
parentNode = [object HTMLDivElement], childNodes = [object
NodeList], attributes = [object NamedNodeMap],
name = undefined, value = undefined, id =
我的问题是,当我的 onclick 处理程序被调用时,我需要获取 4 个包含的 DIV 和 4 个包含的输入 type=text 元素中每一个的 id、名称、值 - 如何获取? 为什么它们都是“未定义”的,因为当我的 onclick() 处理程序被调用时,我确实可以访问 4 个嵌套的 div 和 4 个文本字段。
This should be easy but after studying the DOM and perusing related posts on SO I'm still not there.
I have one outermost div that has, nested within it, 4 divs. And each of the 4 nested divs has one input type="text" nested inside.
And I have a single onclick added to that outermost containing div, so that clicking on any of the 4 nested divs or input type="text" elements invokes the one 'onclick' handler.
And in the onclick handler I need to get the id, name and value of the 4 text inputs and the 4 nested divs too.
Here's the code.
<div class="itemlistRowContainer" id="topmostDiv ID" onclick="handleRowClick(this);"
name="itemlistRowContainer Div name" value="itemlistRowContainer Div Value">
<div class="itemListRowElementLeftMost" name="itemListRowElementLeftMost Div name"
value="itemListRowElementLeftMost Div value">Item<br />
<input type="text" size="30" maxlength="50" id="itemName_id"
name="itemName" value="$itemname" readonly="readonly"></input>
</div>
// 3 more divs and nested text input pairs not shown for brevity
</div>
Here's the onclick event handler -- notice above that in the outermost div, I pass 'this' to the onclick() handler, which from various SO posts I read, I believe 'this' passes the outermost div itself to the onclick handler:
function handleRowClick(theElement)
{
var childNodes = theElement.childNodes;
var numChildNodes = childNodes.length;
alert("handleRowClick: childNodes array has length = " + numChildNodes);
for(i = 0; i < numChildNodes; i++)
{
alert("The DOM element " + i + " has innerHTML = " + childNodes[i].innerHTML + ", "
+ "nodeName = " + childNodes[i].nodeName + ", "
+ "nodeType = " + childNodes[i].nodeType + ", "
+ "nodeValue = " + childNodes[i].nodeValue + ", "
+ "parentNode = " + childNodes[i].parentNode + ", "
+ "childNodes = " + childNodes[i].childNodes + ", "
+ "attributes = " + childNodes[i].attributes + ", "
+ "name = " + childNodes[i].name + ", "
+ "value = " + childNodes[i].value + ", "
+ "id = " + childNodes[i].id);
}
The first alert() above shows me that there are 9 total array elements in the childNodes array.
When the 'for' loop executes, it tells me there are 2 types of DOM elements in the array:
There are 5 reported as: nodeName=#text, nodeType = 3, and value=id=name=undefined
And there are 4 childNode[] elements reported as: nodeName=DIV, nodeType = 1, and AGAIN -- value=name=id=undefined.
In my opinion -- it looks like in the onclick handler, I'm correctly being passed the 4 nested divs (nodeType 1) and the 4 read-only text fields, nodeType=#text, nodeType=3, input type=text.
And then the 9th and final element reported by the 'for' loop is -- one extra nodeName=#text element (nodeType = 3) that I have no clue what that is -- if it was the outermost div then it would have nodeType=1 and nodeName=div like the 4 inner divs.
Fine and dandy. But then why the heck are the id, the name and value unable to help me figure out which is which?
The innerHTML, for the DIV nodes, has the html tags for the input type=text nested within but I have no plans to parse it -- the output (in the alert() box of the 'for' loop above) for a DIV node is this:
The DOM element 1 has innerHTML = Item<br>
<input size="30" maxlength="50" id="itemName_id" name="itemName"
value="f's first item" readonly="readonly"
type="text">
, nodeName = DIV, nodeType = 1, nodeValue = null,
parentNode = [object HTMLDivElement], childNodes = [object
NodeList], attributes = [object NamedNodeMap],
name = undefined, value = undefined, id =
My question is, when my onclick handler is called I need to get the id, name, value of each of the 4 contained DIVs and the 4 contained input type=text elements -- how?
Why are they all 'undefined' because it sure looks like I'm getting access to the 4 nested divs and the 4 text fields when my onclick() handler is called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最外面的 div 有 4 个子 div。因此,
theElement.childNodes.length
应该为您提供 4。这是您看到的 4 个 nodeName = DIV、nodeType = 1 结果。每个输入元素都是内部 div 的子元素。theElement.childNodes
将不会返回输入元素。神秘的nodeType = 3条目是文本节点。考虑以下片段外部 div 有两个子节点 - 一个嵌套 div 和文本
text in external div
尝试使用
Your outermost div has 4 child divs. So
theElement.childNodes.length
should give you 4. These are the 4 nodeName = DIV, nodeType = 1 results that you see. Each input element is a child of the inner div.theElement.childNodes
will not return the input elements. The mysterious nodeType = 3 entries are text nodes. Consider the following fragmentThe outer div has two child nodes - a nested div and the text
text in outer div
try using
使用 jquery.Eg 的
parent()
和siblings
和children()
函数$('@idOfOuterDiv').children().html()
这将给出内部 div 内容。按照相同的方式获取所有内容Use the
parent()
andsiblings
andchildren()
function of jquery.E.g.$('@idOfOuterDiv').children().html()
this wud give the inner div content.Follow same way u get all the conents您可以使用
HTMLElement.getAttribute(attributeName)
获取属性值You can use
HTMLElement.getAttribute(attributeName)
to get attribute values