如何通过自定义HTML ID动态删除或添加元素?

我能够在后面的代码中放置一个具有自定义ID的div,我需要知道如何删除或向自定义div添加更多内容...

我尝试过locate.InnerHTML,但是当然没有用。

还有ClientScript.RegisterStartupScript(this.GetType(),"remove","<script>$('" + locate + "').remove();</script>");,什么都没发生。


<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Test2.aspx.cs" Inherits="StepFollowingDemo.WebForm1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <br />
    <asp:button runat="server" type="button" ID="BtnAdd" Text="Add more" class="btn btn-basic"/>
    <asp:button runat="server" type="button" ID="BtnAdd2" Text="Add Dual" class="btn btn-warning"/> <!--This button is meant to delete the last <div> placed by the id ignore the name for now-->
    <hr />
    <div runat="server" class="well container">
        <div id="TestDiv" runat="server" class="row">

        </div>
    </div>

    <br />
    <div class="well">
        <div class="row">
            <div class="well col-sm-1">1.</div>
        </div>
        <div class="row">
            <div class="well col-sm-1">a1.</div>
            <div class="well col-sm-1">b1.</div>
        </div>
    </div>

</asp:Content>


using System;
using System.Collections.Generic;
using System.Linq;
using system.web;
using system.web.UI;
using system.web.UI.WebControls;

namespace StepFollowingDemo
{
    public partial class WebForm1 : system.web.UI.Page
    {
        static int counter = 1;
        string count = counter.ToString(); //Step count
        string locate = "add"+(counter - 1).ToString(); //Locate last step by id
        public void Page_Load(object sender,EventArgs e)
        {
            BtnAdd.Click += BtnAdd_Click; //Add singular step
            BtnAdd2.Click += BtnAdd2_Click; //Add a two way step
        }

        private void BtnAdd2_Click(object sender,EventArgs e)
        {
            //Response.Write("<script>alert('"+locate+"')</script>"); //Alert to know wich one is the latest step
            //ClientScript.RegisterStartupScript(this.GetType(),"<script>$('" + locate + "').remove();</script>");
        }

        private void BtnAdd_Click(object sender,EventArgs e)
        {
            counter++;
            TestDiv.InnerHtml += "<div id='add"+ count +"'runat='server' class='col-sm-4'>";
            TestDiv.InnerHtml += "<h3>"+count+". </h3>";
            TestDiv.InnerHtml += "<p>Lorem ipsum dolor sit amet,consectetur adipiscing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,sunt in culpa qui officia deserunt mollit anim id est laborum.</p>";
            TestDiv.InnerHtml += "</div>";
        }

    }
}

此按钮需要删除通过捕获生成的自定义ID放置的项目,如果你们还有我可以做的其他事情,将不胜感激!

sunyinglu 回答:如何通过自定义HTML ID动态删除或添加元素?

我将使用asp:panel而不是div。

然后,我们可以使用受保护的字符串来编写您的html输出(包括div),并在按钮单击事件上对其进行更改。 StringBuilder将帮助在文件后面的代码中创建该输出。

根据更改和数据的复杂性,我可能会使用其他方法,但是我认为这将是一个不错的开始,应该可以帮助您完成想要完成的工作。

,

假设这一行是jquery:

ClientScript.RegisterStartupScript(this.GetType(),“删除”,“ $('” +定位+“').remove();”);

您并没有将要删除的元素的ID作为目标,因为您不能仅仅将ID放入其中,所以必须告诉jquery这是一个ID。要将ID定位到div元素以进行删除,您需要以下代码:

ClientScript.RegisterStartupScript(this.GetType(),“删除”,“ $('div#” +定位+“').remove();”);

jQuery文档:https://api.jquery.com/remove/https://api.jquery.com/id-selector/

本文链接:https://www.f2er.com/3128156.html

大家都在问