我怎样才能获得“id”?共 4
4<输入类型 =“文本”>

发布于 2025-01-06 23:15:57 字数 3425 浏览 0 评论 0原文

这应该很容易,但是在研究了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰火雁神 2025-01-13 23:15:57

最外面的 div 有 4 个子 div。因此,theElement.childNodes.length 应该为您提供 4。这是您看到的 4 个 nodeName = DIV、nodeType = 1 结果。每个输入元素都是内部 div 的子元素。 theElement.childNodes 将不会返回输入元素。神秘的nodeType = 3条目是文本节点。考虑以下片段

<div>
   <div>text in innerdiv</div>
   text in outer div   
</div>

外部 div 有两个子节点 - 一个嵌套 div 和文本 text in external div

尝试使用

function handleRowClick(theElement)
{

    var childDivs = theElement.getElementsByTagName("div");
    var childInputs = theElement.getElementsByTagName("input");

    for(i = 0; i < childDivs.length; i++)
    {
        //process child divs here
    }

    for (i=0; i<childInputs.length; i++ )
    {
        //process child inputs here
    }
 }

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 fragment

<div>
   <div>text in innerdiv</div>
   text in outer div   
</div>

The outer div has two child nodes - a nested div and the text text in outer div

try using

function handleRowClick(theElement)
{

    var childDivs = theElement.getElementsByTagName("div");
    var childInputs = theElement.getElementsByTagName("input");

    for(i = 0; i < childDivs.length; i++)
    {
        //process child divs here
    }

    for (i=0; i<childInputs.length; i++ )
    {
        //process child inputs here
    }
 }
瑕疵 2025-01-13 23:15:57

使用 jquery.Eg 的 parent()siblingschildren() 函数
$('@idOfOuterDiv').children().html() 这将给出内部 div 内容。按照相同的方式获取所有内容

Use the parent() and siblings and children() function of jquery.E.g.
$('@idOfOuterDiv').children().html() this wud give the inner div content.Follow same way u get all the conents

留一抹残留的笑 2025-01-13 23:15:57

您可以使用 HTMLElement.getAttribute(attributeName) 获取属性值

You can use HTMLElement.getAttribute(attributeName) to get attribute values

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文