简体   繁体   English

我怎么能清空这个div?

[英]How can I empty this div?

I write a div and set contenteditable is true, so it can be editable. 我写了一个div并且set contenteditable是真的,所以它可以编辑。

<div id="editor" contenteditable="true"></div>

and first I wrote something in it like "hello" open the console I see the code has changed to 首先我在其中写了一些东西,比如“你好”打开控制台,我看到代码已经改变了

<div id="editor" contenteditable="true">hello</div>

but when I cleared the word I've input in this div and input something new in it again I saw the console, the code was like this: 但当我清除了我在这个div中输入的单词并再输入一些新内容时,我看到了控制台,代码是这样的:

<div id="editor" contenteditable="true">
    "hello"
    ""
</div>

and The times that I delete is "" appears. 我删除的时间是""

I've tried to $("#editor").empty() when detect DOMNodeRemoved but it didn't work.... 我已经尝试过$("#editor").empty()当检测到DOMNodeRemoved但它没有工作....

How can I empty this div? 我怎么能清空这个div?

Simply use below code to empty your div container: 只需使用下面的代码清空div容器:

$("#editor").text('')

Here is sample code that clears the container for you: 以下是为您清除容器的示例代码:

 $('.clear').click(function(){ $('#editor').text('') }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="editor" contenteditable="true"> "hello" "" </div> <button class="clear">clear</button> 

very simple 很简单

$('#editor').html('');

or 要么

jQuery('#editor').html('');

try this 尝试这个

.html('') will work in your case. .html('')将适用于您的情况。 $('#editor').html('');

To listen for changes we can use: 要听取我们可以使用的更改:

$("#editor").on('DOMNodeRemoved', function(e) {
    console.log(e.target, ' => removed');
});

See example: 见例子:

 setTimeout(function() { $('#editor').html(''); }, 1000); $("#editor").on('DOMNodeRemoved', function(e) { console.log(e.target, ' => removed'); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="editor" contenteditable="true"> "hello" "" </div> 

Do something like this 做这样的事情

$("#editor").text(""); - If this selector is a label
$("#editor").val(""); - If this selector is a input control

Sameway you can do for multiple using class names 您可以使用Sameway为多个使用类名

$(".editorClass").text("");
$(".editorClass").val("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM