Html 如何将 div contenteditable 从 true 更改为 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16996060/
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
How to change div contenteditable from true to false
提问by Theng Ming
I got a page which let user change the text inside div and save the html code into a database, however when I display back the html code I want to change the div contenteditable
to false. Is there any way???
我有一个页面,让用户更改 div 中的文本并将 html 代码保存到数据库中,但是当我显示 html 代码时,我想将 div 更改contenteditable
为 false。有什么办法???
<link href='http://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Ultra' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" ; />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="function.js"></script>
<h1>H1</h1>
<div class="drag">
<div id="box" style="padding-left: 45px; background: url(2.png);">
<div class="line-division1" style="margin-top: 100px; background-color: #003663;"></div>
<div id="sub-title" contenteditable="true"><span>YOU'RE IN FOR A</span></div>
<div id="title" contenteditable="true">Wild
<font color="#90a6b9">Ride</font>
</div>
<div class="line-division1" style="margin-top: 20px; background-color: #003663;"></div>
<div class="date1" contenteditable="true">THIS JANUARY 21ST 2014</div>
<div class="date1" contenteditable="true">1337 ACCELERATOR, KL</div>
<div class="line-division1" style="width: 150px; height:22px; float: left; background-color: #003663; margin-top: 50px;"></div>
<div class="line-division1" style="width: 150px; height:22px; float: right; background-color: #003663; margin-top: 50px; margin-right: 50px;"></div>
<div class="date1" style="font-size: 80px; width: 500px;" contenteditable="true">JOIN US</div>
<br>
<div class="date1" style="font-size: 38px; margin-top: 20px;">FOR A RIDE OF YOUR LIFE TIME</div>
<img src="waterm.png" style="zoom:30%; margin-top: 280px; margin-left: 2000px;">
</div>
</div>
回答by squirly
Since you have 5 elements with contenteditable
, try giving them IDs to simplify access.
由于您有 5 个元素contenteditable
,请尝试为它们提供 ID 以简化访问。
The following code deactivates the 5 contentediable
elements:
以下代码停用 5 个contentediable
元素:
var editable_elements = document.querySelectorAll("[contenteditable=true]");
editable_elements[0].setAttribute("contenteditable", false);
editable_elements[1].setAttribute("contenteditable", false);
editable_elements[2].setAttribute("contenteditable", false);
editable_elements[3].setAttribute("contenteditable", false);
editable_elements[4].setAttribute("contenteditable", false);
Or you can you a loop
或者你可以循环
var editable_elements = document.querySelectorAll("[contenteditable=true]");
for(var i=0; i<editable_elements.length; i++)
editable_elements[i].setAttribute("contenteditable", false);
回答by Vond Ritz
Even though you not tagged this question as jquery, you already asked if there is a way to do it. and my way to do it is using jquery.
即使您没有将此问题标记为 jquery,您也已经询问是否有办法做到这一点。我的方法是使用 jquery。
Here's the FIDDLE
这是小提琴
$('div').blur(function () {
$(this).attr('contenteditable', false);
});
or after saving the div content to db. use this code below to edit all div with contenteditable attribute true to false.
或者在将 div 内容保存到 db 之后。使用下面的代码来编辑所有具有 contenteditable 属性 true 为 false 的 div。
Heres' the FIDDLE
这是小提琴
$('div[contenteditable="true"]').attr('contenteditable', false);
回答by Fabien Sa
To do it on each elements with "contenteditable=true" you can simple try this:
要使用“contenteditable=true”对每个元素执行此操作,您可以简单地尝试以下操作:
var editableElements = document.querySelectorAll("[contenteditable=true]");
for (var i = 0; i < editableElements.length; ++i) {
editableElements[i].setAttribute("contentEditable", false);
}
回答by pearl's
Ming,
明,
You can use a Submit button at the end of your Div
. So when the user is done with editing he can submit the form, you can do a post to save this text in database and also the button will make the content non-editable. Here I added a button to your code and a bit of jQuery.
您可以在Div
. 因此,当用户完成编辑后,他可以提交表单,您可以发布帖子将此文本保存在数据库中,并且该按钮将使内容不可编辑。在这里,我在您的代码中添加了一个按钮和一些 jQuery。
HTML
HTML
<div id="box" style="padding-left: 45px; background: url(2.png);">
<div class="line-division1" style="margin-top: 100px; background-color: #003663;"> </div>
<div id="sub-title" contenteditable="true"><span>YOU'RE IN FOR A</span></div>
<div id="title" contenteditable="true">Wild <font color="#90a6b9">Ride</font></div>
<div class="line-division1" style="margin-top: 20px; background-color: #003663;"></div>
<div class="date1" contenteditable="true">THIS JANUARY 21ST 2014</div>
<div class="date1" contenteditable="true">1337 ACCELERATOR, KL</div>
<div class="line-division1" style="width: 150px; height:22px; float: left; background- color: #003663; margin-top: 50px;"></div>
<div class="line-division1" style="width: 150px; height:22px; float: right; background-color: #003663; margin-top: 50px; margin-right: 50px;"></div>
<div class="date1" style="font-size: 80px; width: 500px;" contenteditable="true">JOIN US</div>
</br>
<div class="date1" style="font-size: 38px; margin-top: 20px;">FOR A RIDE OF YOUR LIFE TIME</div>
<button text="Submit">Submit</button>
jQuery(just include a jquery file to your code)
jQuery(只需在代码中包含一个 jquery 文件)
$(function(){
$('button').click(function(){
$('div').attr('contenteditable', 'false');
});
});
Thats it !!!
就是这样 !!!
回答by bugwheels94
This work in all browser
这适用于所有浏览器
var arr=document.getElementsByTagName("div");
for(var i=0;i<document.getElementsByTagName("div").length;i++)
document.getElementsByTagName("div")[i].removeAttribute("contenteditable");
This will work in modern browser(but too much versatile)
这将适用于现代浏览器(但用途太多)
for(var i=0;i<document.querySelectorAll("div").length;i++)
document.querySelectorAll("div")[i].removeAttribute("contenteditable");