zoukankan      html  css  js  c++  java
  • document.createElement后面带的参数

    原来只知道document.createElement后面带HTML元素这种用法,如
      var oTD2=document.createElement("TD");
    今天要对增加的元素使用document.getElementsByName获取其数组,因此对增加的元素name属性的设置:
     var oCk1=document.createElement("INPUT");
     oCk1.type="checkbox";
     oCk1.name="ChkSel";
     //oCk1.setAttribute("name", "ChkSel");

    但以上代码却没有成功(也没有出错)。


    开头还以为是bug之类的,后来查createElement的帮助,发现一句话
    Attributes can be included with the sTag as long as the entire string is valid HTML. You should do this if you wish to include the NAME attribute at run time on objects created with the createElement method.
    就是说document.createElement带的参数不只是一个HTML元素,也可以用HTML,这样,name属性就可以加入,例如:
    var newRadioButton = document.createElement("<INPUT TYPE='RADIO' NAME='RADIOTEST' VALUE='First Choice'>")

    因此,我的代码改成如下即可:
     var oCk1=document.createElement("<input type=\"checkbox\" name=\"ChkSel\" />");
     oTD1.appendChild(oCk1);


     

  • 相关阅读:
    错误记录笔记(持续更新)
    聊聊iOS Keychain
    IDEA下将dubbo简单项目跑Demo(2019.12版)
    dubbo控制台启动步骤
    day1
    信息安全
    huZABtwIrp
    【踩坑】环境问题
    【踩坑】IDEA 设置 JVM 参数
    WebServer Project-02-XML解析
  • 原文地址:https://www.cnblogs.com/yzx99/p/1274504.html
Copyright © 2011-2022 走看看