【问题标题】:Inserting String as Elements to the DOM for javascript?将字符串作为元素插入 DOM 以获取 javascript?
【发布时间】:2011-04-13 05:43:19
【问题描述】:

我有一个测试的 HTML 块,例如::

HTML 是一个字符串,而不是 DOM 元素,但我正在尝试查看是否有一种方法,或者一种可用于将字符串作为 DOM 插入的方法,因此可以将其附加到 DOM .

var test='<tr class="rowHeaders">';
test=test+'<td id="sTD" name="sTD" width="4%">test.php</td>'
test=test+'<td width="2%"><input type="radio" name="tb" ></td>';
test=test+'<td id="tTD" name="tTD" width="2%">php</td>';
test=test+'<td width="2%"><input type="button" name="vv" ></td>';
test=test+'</tr>';


var scriptTBL=document.getElementById("scriptsT");

scriptTBL.children[0].appendChild(test);

尝试做这样的事情......

但是“test”不是一个有效的节点或元素,那么我怎样才能将它添加到元素中呢??

我考虑过使用 innerHtml,但对于 DOM 的 table/tbody 可能已经有一个现有的子项。

我一直在探索片段,但这不是点击!

测试 html 有 tbl:

<table id="scriptsT" name="scriptsT" >
  <tr>
    .
    .

非常感谢您的指点或想法。

谢谢。

【问题讨论】:

    标签: javascript dom


    【解决方案1】:

    您可以附加到innerHTML:

    scriptTBL.tBodies[0].innerHTML += test;
    

    foo += barfoo = foo + bar 的简写。您还可以通过这种方式简化您的 HTML 创建代码。使用test += 'html here';

    appendChild 只接受一个 DOM 元素。

    【讨论】:

    • 为了避免不必要的 DOM 交互(以及对未闭合标签的陌生感),您应该附加到一个字符串变量,然后在完成后使用完整的字符串设置 innerHTML。
    • @Zach: 是的,scriptTBL.tBodies[0].innerHtml += test 中的test 应该是最后的test 变量......但你指出来是对的。
    • 我明白了——这是有道理的。另外,我认为它应该是 .innerHTML,而不是 .innerHtml。
    【解决方案2】:

    创建一个div(或span 或其他)并使用innerHTML 加载您的片段。

    var someDiv = document.createElement("div");
    someDiv.innerHTML = "<tr ....  ";
    someParentElement.appendChild(someDiv);
    

    【讨论】:

      【解决方案3】:

      你可以这样做:

      var test = '<tr class="rowHeaders">';
      test = test + '<td id="sTD" name="sTD" width="4%">test.php</td>'
      test = test + '<td width="2%"><input type="radio" name="tb" ></td>';
      test = test + '<td id="tTD" name="tTD" width="2%">php</td>';
      test = test + '<td width="2%"><input type="button" name="vv" ></td>';
      test = test + '</tr>';
      
      var discardableElement = document.createElement("div");
      discardableElement.innerHtml = test;
      
      var scriptTBL = document.getElementById("scriptsT");
      scriptTBL.tBodies[0].appendChild(discardableElement.firstChild);
      

      这有点浪费,因为您创建 DOM 元素(这是一项昂贵的操作)只是为了丢弃它,但它会创建 DOM 元素以允许您使用 appendChild 方法。

      或者,您可以使用字符串连接来使用 innerHtml 属性,如下所示:

      var test = '<tr class="rowHeaders">';
      test = test + '<td id="sTD" name="sTD" width="4%">test.php</td>'
      test = test + '<td width="2%"><input type="radio" name="tb" ></td>';
      test = test + '<td id="tTD" name="tTD" width="2%">php</td>';
      test = test + '<td width="2%"><input type="button" name="vv" ></td>';
      test = test + '</tr>';
      
      
      var scriptTBL = document.getElementById("scriptsT");
      
      // Insert at the BOTTOM of the table
      var newHtml = scriptTBL.innerHtml + test;
      
      // OR... Insert at the top of the table
      //var newHtml =  test + scriptTBL.innerHtml;
      
      scriptTBL.tBodies[0].innerHtml = newHtml; 
      

      编辑:根据@Zach 的评论更新。

      【讨论】:

      • 我认为@Zach 有不同的含义。 scriptTBL.innerHtml += newHtml scriptTBL.innerHtml = scriptTBL.innerHtml + newHtml;
      • @Felix - 是的,我在编辑后也是这么想的(事后我觉得很愚蠢),但这样看起来更漂亮。
      猜你喜欢
      • 2014-11-10
      • 2012-11-18
      • 2015-04-22
      • 2016-10-20
      • 1970-01-01
      • 2012-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多