JS无限添加HTML到指定位置

用JS把HTML添加到指定位置有两种写法,一种是用字符串,一种是用javascript中的方法

第一种:

用字符串写

<h2>利用JS无限添加一个相同部分</h2>
        <h5>第一种写法</h5>
        <%--有多少个相同的这里面的值就是几--%>
        <asp:HiddenField ID="hfCount" runat="server" Value="-1" />
        <%--每个就是后面的数字不一样 例如:One1,One2,One3....      --%>
        <asp:HiddenField ID="hfIndex" runat="server" />
        <div id="divOne" runat="server">       <%-- 这里添加HTML --%>
        </div>
        <span class="addto"><a href="javascript:void(0);" onclick="AddInfo();">添加</a></span>
        <br />
        <hr />
  <script type="text/javascript">
            $(function () {
                var count = $("#hfCount").val();
                if (count <= 0) {
                    AddInfo();
                }
            })

            function AddInfo() {
                //根据数量来增加   hfCount的值
                var Count = $("#hfCount").val();  //先取值
                var PageIndex = 0;
                //如果没有就自己加,有就根据个数来定
                if (Count > -1) {
                    var Indexs = $("#hfIndex").val().split(‘,‘);  //有多少个dl
                    Count = ++Indexs[Count - 1];  //第几个  取最大值 +1就可以
                    PageIndex = Indexs.length - 1;  //个数

                } else {
                    Count++;
                    PageIndex = Count;
                }

                var strHtml = "\r\n";
                strHtml += "<dl class=‘form_main‘ id=\"Form" + Count + "\">\r\n";
                //这里绑定表的主键  好做修改
                strHtml += "<input type=\"hidden\" name=\"hfID" + Count + "\" value=\"0\"/>\r\n";

                strHtml += "<dd>\r\n";
                strHtml += "<u>测试数据一:</u>\r\n";
                strHtml += "<input type=‘text‘ id=‘One" + Count + "‘ name=‘One" + Count + "‘ />\r\n";
                strHtml += "</dd>\r\n";

                strHtml += "<dd>\r\n";
                strHtml += "<u>测试数据二:</u>\r\n";
                strHtml += "<input type=‘text‘ id=\"Two" + Count + "\" name=‘" + Count + "‘ />\r\n";
                strHtml += "</dd>\r\n";

                if (Count > 0) {
                    strHtml += "<dt class=\"delete\"><a href=\"javascript:void(0);\" onclick=\"delControl(‘Form" + Count + "‘)\" >删除</a></dt>\r\n";
                }
                strHtml += "<dt></dt>\r\n";
                strHtml += "</dl>";                  //字符串拼接好
                $("#divOne").append(strHtml);
                $("#hfCount").val(++PageIndex);
                var dd = $("#hfIndex").val();
                dd += Count + ",";
                $("#hfIndex").val(dd);
            }

             //删除
            function delControl(id) {
                $("dl").remove("#" + id);
                //我们让下面的ID删除,数量减一就可以了
                var count = $("#hfCount").val(); //取值
                $("#hfCount").val(--count);    //计算后赋值
                var index = $("#hfIndex").val();  //0,1,2,3
                index = index.replace(id.replace("Form", "") + ",", ""); //删除第几个就把 x, 删除
                $("#hfIndex").val(index);
            }

4代表是有多少个,下面的0,1,4,5,代表dl里面的数字   这个是对应的,后台取值需要

第二种,利用Javascript创建元素来写

   <h5>第二种写法</h5>
        <div id="divHtml" runat="server">
        </div>
        <asp:HiddenField ID="hfTest" Value="0" runat="server" />
        <span class="addto"><a href="javascript:void(0);" onclick="AddHtml();">添加</a></span>
      function AddHtml() {
                var count = $("#hfTest").val();
                var divHtml = document.getElementById("divHtml");
                var newdl = document.createElement("dl"); //创建一个元素
                newdl.setAttribute("class", "form_main");   //设置属性
                newdl.setAttribute("id", "FormOne" + count);

                //创建第一个dd
                var newdd0 = document.createElement("dd"); //创建一个子元素

                //u
                var newu0 = document.createElement("u");
                newu0.innerHTML = "测试数据One";  //设置文本
                newdd0.appendChild(newu0);   //dd里面添加 u
                //input
                var newinput0 = document.createElement("input");
                newinput0.setAttribute("id", "DataOne" + count);
                newinput0.setAttribute("Name", "DataOne" + count);
                newinput0.setAttribute("type", "text");

                newdd0.appendChild(newinput0);   //dd里面添加 input
                newdl.appendChild(newdd0);          //dl里面添加dd

                //第二个
                var newdd1 = document.createElement("dd");

                ////u
                var newu1 = document.createElement("u");
                newu1.innerText = "测试数据Two";
                newdd1.appendChild(newu1);
                //input
                var newinput1 = document.createElement("input");
                newinput1.setAttribute("type", "text");
                newinput1.setAttribute("id", "DataTwo" + count);
                newinput1.setAttribute("Name", "DataTwo" + count);

                newdd1.appendChild(newinput1);
                newdl.appendChild(newdd1);
                if (count > 0) {
                    //创建删除
                    var newdeldt = document.createElement("dt");
                    newdeldt.setAttribute("class", "delete");
                    var newa = document.createElement("a");
                    newa.setAttribute("href", "javascript:void(0)");
                    newa.setAttribute("onclick", "deleteHtml(‘FormOne" + count + "‘)");
                    newa.innerHTML = "删除";
                    newdeldt.appendChild(newa);
                    newdl.appendChild(newdeldt);
                }
                var dt = document.createElement("dt");
                newdl.appendChild(dt);
                divHtml.appendChild(newdl);  //把dl添加到div里面
                count++;
                $("#hfTest").val(count);
            }

            function deleteHtml(id) {
                $("#" + id).remove();
                var count = $("#hfTest").val();
                count--;
                $("#hfTest").val(count--);               //个数   并保证 dl的id以xx0,xx1,xx2,xx3的规律
                var i = 0;
                $("#divHtml dl").each(function () {                    //设置每个dl的属性,方便后台取值
                    $(this).attr("id","FormOne" + i);
                    $(this).children("dd:eq(0)").find("input").attr({ "id": "DataOne" + i, "name": "DataOne"+i });
                    $(this).children("dd:eq(1)").find("input").attr({ "id": "DataTwo" + i, "name": "DataTwo" + i });
                    $(this).children("dt[class=‘delete‘]").attr("onclick", "deleteHtml(‘FormOne" + i + "‘)");
                    i++;
                })
            }

页面

生成的HTML,id很有规律

时间: 2024-08-25 13:39:57

JS无限添加HTML到指定位置的相关文章

如何在JS数组特定索引处指定位置插入元素?

需求: 将一个元素插入到现有数组的特定索引处.听起来很容易和常见,但需要一点时间来研究它. // 原来的数组var array = ["one", "two", "four"];// splice(position, numberOfItemsToRemove, item)// 拼接函数(索引位置, 要删除元素的数量, 元素)array.splice(2, 0, "three"); // www.jbxue.comarray;

js将滚动条滚动到指定位置的方法

代码如下(主要是通过设置Location的hash属性): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <

vbscript input select 添加个option根据value值到指定位置--相当于排序

'添加option到指定位置(按value排序) dim valindex valindex=-1 for i=0 to selcom.length-1 if selcom.Options(i).value >selallcomoption.value then valindex=i exit for end if next dim oOption set oOption= document.CreateElement("OPTION") oOption.text = selal

js 页面滚动到指定位置

项目中,本来想通过点击左侧列表让页面进行快速导航(根据 id="item" 和 a标签的 href 属性[<a href="#item"></a>]),不过滚动后的页面一直是顶在浏览器页面顶部的,不符合项目需求,所以就通过 js 代码让页面进行指定位置滚动,代码如下: $(".left_nav>ul>li").click(function () { var n = parseInt($(this)[0].id)

用js控制按钮时间,将指定内容添加到文本域的光标所在位置。

<pre class="javascript" name="code"> var fm = document.getElementsByTagName('form')[0]; var button = document.getElementsByName("button"); //获取一个cookie的值 function getCookie(index){ var allcookies = document.cookie; var

js数组指定位置添加和删除元素

//按指定位置删除Array.prototype.removeIndex = function (index) { if (index > - 1) { this.splice(index, 1); } };//按元素名称删除 Array.prototype.remove = function (val) { var index = this.indexOf(val); if (index > - 1) { this.splice(index, 1); } };//添加元素到指定位置 Arra

js,jquery滚动/跳转页面到指定位置

要解决两个需求: 一个是从A页面跳到B页面,滚动到页面的任何地方: 第二个是在B页面内部点击某个元素,滚动到页面的任何地方: 怎么解决啊?很简单,当然是用锚点. 首先在A页面创建一个锚点 <body> <a href="b.html#pos" target="_blank">点击跳转</a> <body> 然后在B页面定义这个锚点 <body> ... 这里是很多文字,把页面撑开,撑出滚动条 ... <

html js点击按钮滚动跳转定位到页面指定位置(DIV)的方法代码

一:通过html锚点实现滚动定位到页面指定位置(DIV):    如果我们要点击实现跳转的地方是一个html锚点,也就是点击一个A标签超链接实现跳转,可以把A标签的href属性直接指向跳转指定位置的div,代码实现思路如下: <a class="banner" href="/schoolFair/registration#nav"> <a href="#abc">点击跳转</a>    <div id=&

js获取一个字符串中指定字符串第n次出现的位置

1.JS获取一个字符串中指定字符串第n次出现的位置 了解类似的获取字符位置的方法: 1.1 charAt() 获取字符串指定位置的字符 用法:strObj是字符串对象,index是指定的位置,(位置从0开始数) strObj.charAt(index) 1.2 indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置 用法:stringObject是字符串对象,searchvalue是指定的字符串值,fromindex(可有可无)指定开始匹配字符串值的位置,若无,表示从0位置开始