简体   繁体   English

即使元素确实有子节点,方法中的 hasChildNodes() 也会返回 false

[英]hasChildNodes() in method returns false even though the element does have child nodes

I'm making a quiz creation app that allows me to choose a text area, a text box, a select box, or a set of radio boxes for a specific question.我正在制作一个测验创建应用程序,它允许我为特定问题选择一个文本区域、一个文本框、一个 select 框或一组单选框。 I can add and remove all those input types fine except for the radio boxes.除了单选框之外,我可以很好地添加和删除所有这些输入类型。 I'm trying to delete individual radio boxes, and to do that I want to use a dynamically generated, numbered DIV element that the radio button, label, and delete button can all append to.我正在尝试删除单个单选框,为此我想使用一个动态生成的编号 DIV 元素,单选按钮 label 和删除按钮都可以 append 到。 Here is the relevant HTML.这是相关的 HTML。 Each generated DIV with this content will be appended to the DIV with the formDiv ID.每个生成的具有此内容的 DIV 都将附加到具有 formDiv ID 的 DIV。

   <div style='margin-top: 10px;' class='jumbotron'>
       <form action='#' method='#'>
           <div>
    <input type=’button’ value=’Add component’ id=’addContent’>
    <div id=’chooseContent’ style=’display:none’>
        <ul>
            <li class=’list-group-item’>
                <div id=”textareaOpt”>Text area</div>
            </li>
            <li class=’list-group-item’>
                <div id=”textOpt”>Text box</div>
            </li>
            <li class=’list-group-item’>
                <div id=”selectOpt”>Select</div>
            </li>
            <li class=’list-group-item’>
                <div id=”radioOpt”>Radio</div>
            </li>
        </ul>
        <input type=’button’ id=’cancelContent’ value=’X’>
    </div>
               <div id= 'formDiv'></div>
           </div>
       </form>
   </div>

The formDiv element is meant to be the parent element to any input type. formDiv 元素是任何输入类型的父元素。 I'm dynamically adding radio buttons to a page that are appended to the generated DIV component, and use addDeleteBtn() to create a button to remove the component and I'm trying to append it to the component instead of formDiv.我正在将单选按钮动态添加到附加到生成的 DIV 组件的页面中,并使用 addDeleteBtn() 创建一个按钮以删除该组件,并且我正在尝试将其 append 到组件而不是 formDiv。 However, addDeleteBtn() doesn't interpret formDiv as having any child elements for it to append to, even though createComponent() does recognize formDiv's children.但是, addDeleteBtn() 不会将 formDiv 解释为具有 append 的任何子元素,即使 createComponent() 确实识别 formDiv 的子元素。

function createComponent(){
if(targetType == 'radio'){
  form_content_num++; 
  var component = document.createElement('div');
  component.id = form_content_num; //to dynamically number these divs
  console.log(component);

  var radioLabel = document.createElement('label');
  radioLabel.style.display = 'inline';

  var radioBtn = document.createElement('input');
  radioBtn.style.display = 'inline';
  radioBtn.type = targetType;

  component.appendChild(radioLabel);
  component.appendChild(radioBtn);
  console.log("formDiv.childNodes[0] " + formDiv.childNodes[0]); //this returns true
}

addDeleteBtn();
formDiv.appendChild(component);
console.log("formDiv.childNodes[0] " + formDiv.childNodes[0]); //also returns true
}
function addDeleteBtn(){
  var formDiv = document.getElementById('formDiv');
  console.log("formDiv " + formDiv);
  console.log("hasChildNodes " + formDiv.hasChildNodes()); //returns false
  //code not relevant to this problem was deleted to save space
}
addDeleteBtn();
formDiv.appendChild(component);

are in the wrong order.顺序错误。 Change it to将其更改为

formDiv.appendChild(component);
addDeleteBtn();

Basically when moving the button to the component from formDiv , the call to addDeleteBtn wasn't moved to after component was added to the DOM.基本上,当将按钮从formDiv移动到component时,在将component添加到 DOM 之后,对addDeleteBtn的调用不会移动到。

暂无
暂无

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

相关问题 即使数组返回节点,addEventListener 也不是 function? - addEventListener is not a function even though array returns nodes? hasChildNodes 返回 false 但 childNodes 返回一个节点列表,如 html - hasChildNodes returns false but childNodes returns a node list as in the html 使用Rangy,即使我通过setStartBefore方法扩展范围,range.canSurroundContents方法仍然返回false - With Rangy, even though I extend the range by setStartBefore method, range.canSurroundContents method still returns false 视频元素静音在Chrome中返回false,即使属性设置为“静音” - Video element muted returns false in Chrome, even though attribute is set to “muted” (event.Target === document.getElementById(“id”)) 返回 false,即使它们指向同一个元素 - (event.Target === document.getElementById(“id”)) returns false even though they are pointing at the same element ajax 测试返回 false,即使 http 请求确实包含 XMLHttpRequest 参数 - ajax test returns false even though the http request does contain the XMLHttpRequest parameter 即使数组中存在元素,inArray()也会返回-1 - inArray() returns -1 even though element exists in array 即使元素在DOM中,getElementsByClassName仍返回未定义 - getElementsByClassName returns undefined even though the element is in the DOM 子道具没有更新,即使父母更新了 - Child prop not updating, even though the parent does 即使身份验证成功,PassportJS Facebook登录isAuthenticated也会返回false - PassportJS Facebook login isAuthenticated returns false even though authentication succeeds
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM