zoukankan      html  css  js  c++  java
  • javascript和jquery动态创建html元素

    1.javascript创建元素

        创建select

            var select = document.createElement("select");
            elect.options[0] = new Option("加载项1", "value1");
           select.options[1] = new Option("加载项2", "value2");
           select.size = "2";
           testDiv.appendChild(select);
        创建div

            var openDiv = document.createElement("div");
            openDiv.id = "div3D";
            openDiv.style.width = w+"px";
            openDiv.style.height = h+"px";
            openDiv.innerHTML = strHtml;
            document.body.appendChild(openDiv);

    2.jquery创建元素

     function CreateDom() {
                   var select = $("<select/>").appendTo($("body"));
                var option1 = $("<option value=\"1\">text1</option>").appendTo(select);
                var option2 = $("<option value=\"2\">text2</option>").appendTo(select);
                var option3 = $("<option value=\"3\">text3</option>").appendTo(select);
                var text = $("<input type=\"text\">").css({ "width": "150px", "border": "1px lightgrey solid" }).appendTo($("body"));
                var checkbox = $("<input type=\"checkbox\" />").appendTo($("body"));
                var ul = $("<ul/>").appendTo($("body"));
                var li = $("<li>li1</li>").appendTo(ul);
                var li = $("<li>li2</li>").appendTo(ul);

      }

  • 相关阅读:
    C++常变量
    C++变量(C++变量定义、变量赋值、命名规则)
    463. Island Perimeter
    500. Keyboard Row
    811. Subdomain Visit Count
    901. Online Stock Span
    419. Battleships in a Board
    620. Not Boring Movies
    893. Groups of Special-Equivalent Strings
    575. Distribute Candies
  • 原文地址:https://www.cnblogs.com/ajun/p/2549091.html
Copyright © 2011-2022 走看看