javascript 使 DIV 中的内容不可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21114077/
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
Make content in DIV uneditable
提问by Mike Badazz
I want to make the contents within a DIV uneditable but still viewable.
我想让 DIV 中的内容不可编辑但仍可查看。
<div id="committee"><?php include 'committee_members_list.php'; ?></div>
The DIV holds the contents of an 'included' file within it. I would like the contents to be viewable but not editable or clickable.
DIV 在其中包含“包含”文件的内容。我希望内容可查看但不可编辑或可点击。
The content of the DIV looks as above.
DIV 的内容如上所示。
NB: The text and textarea elements are not in the included file that's in the DIV
注意:文本和文本区域元素不在 DIV 中的包含文件中
Everything in the DIV should be viewable as it is but nothing should be clickable/editable. How can i achieve/implement this?
DIV 中的所有内容都应该可以按原样查看,但没有任何内容应该是可点击/可编辑的。我怎样才能实现/实现这一点?
回答by Realit?tsverlust
A div is uneditable in general. If you want a textbox which is uneditable, add "readonly":
div 通常是不可编辑的。如果您想要一个不可编辑的文本框,请添加“只读”:
<input type="textbox" name="test" readonly>
Edit: Or you can block it via CSS with the properties user-select:
编辑:或者您可以通过 CSS 使用 user-select 属性阻止它:
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
Remember that this is NOT a validation, i can modify whats inside with firebug or similar tools.
请记住,这不是验证,我可以使用 firebug 或类似工具修改内部内容。
回答by Steini
Well, you could make all elements in a div container uneditable via JQuery if you want it fast and dynamic.
好吧,如果你想要快速和动态的,你可以通过 JQuery 使 div 容器中的所有元素都不可编辑。
<div id="committee">
<?php include 'committee_members_list.php'; ?>
</div>
<script type="text/javascript">
$("#committee").each(function() {
$(this).attr("readonly", "1");
});
</script>
For more security you have to validate everything serversided anyway if it is a form anyway.
为了获得更高的安全性,如果它是表单,则无论如何都必须验证服务器端的所有内容。
But are input elements nesessary anyway if the content is for read only?
但是,如果内容是只读的,那么输入元素是否必要?