使用 JQuery 删除 Div 的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1141623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:45:02  来源:igfitidea点击:

Remove the Contents of a Div using JQuery

jquery

提问by Mercy

How to remove the contents of a Div Using JQuery. For eg.

如何使用 JQuery 删除 Div 的内容。例如。

 <div id="1">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>

i want to remove the full content of the Div . and to add new contents to it.

我想删除 Div 的全部内容。并为其添加新内容。

回答by Darko Z

$("#1").empty();

Also you shouldnt really start id's with a number

此外,您不应该真正以数字开头 id

回答by Jason

$("#1").html("");

or just

要不就

$("#1").html("your new content");

change id to start w/a letter

将 id 更改为以字母开头

回答by SolutionYogi

First, according to the spec, id must start with a letter.

首先,根据规范, id 必须以字母开头。

 <div id="myDiv">
  <label id="label1"/><br/>
  <input type="text" data-attr="phoneEuro" style="width: 200px;" id="input1"/> <label id="instr1"/>
 </div>


$('#myDiv').html('your new content');

If you use html method, you can simply override the existing DIV content.

如果您使用 html 方法,您可以简单地覆盖现有的 DIV 内容。

回答by Keith Donegan

$(document).ready(function()
{
    $('div#one').children().remove();
});

回答by lfx

$("#1").html("<span class='red'>Hello <b>Again</b></span>");

From doc.jquery.com

来自doc.jquery.com