【发布时间】:2015-10-07 23:17:03
【问题描述】:
我遇到了 div 元素高度升高的问题。要查看该问题,您必须插入新元素 “法律案例框”。然后你必须点击右上角的div / 左上角的div。您可以键入 1 个字母,然后将其删除(或仅设置焦点并删除它)-> 您可以看到 div 元素的上升。我不明白为什么会这样。对不起,我的英语不好。谢谢!
我还注意到,如果不评论 innerDiv1.setAttribute("data-text", "Full Name of the Case");,就会发生这种情况
Safari浏览器出现问题。
[contentEditable=true]:empty:not(:focus):before {
content:attr(data-text)
}
.boxed {
margin: 5px;
border: 2px solid black;
max-width: calc(100% - 10px);
}
.inline {
display: inline-block;
vertical-align: middle;
max-width: calc(100% - 10px);
}
.divTable {
display: table;
border-collapse: separate;
border-spacing: 5px;
}
.divRow {
display:table-row;
}
.divCell {
display:table-cell;
border: 2px solid black;
}
<script>
function insert(elem) {
document.getElementById("mainDiv").focus();
var sel = window.getSelection();
var range = sel.getRangeAt(0);
range.insertNode(elem);
}
function legalCaseBox() {
var innerDiv1 = document.createElement('div');
innerDiv1.contentEditable = 'true';
innerDiv1.style.minWidth = '50pt';
innerDiv1.style.minHeight = '50pt';
innerDiv1.classList.add('divCell');
//innerDiv1.setAttribute("data-text", "Full Name of the Case");
var innerDiv2 = document.createElement('div');
innerDiv2.contentEditable = 'true';
innerDiv2.style.minWidth = '50pt';
innerDiv2.style.minHeight = '50pt';
innerDiv2.classList.add('divCell');
innerDiv2.setAttribute("data-text", "Year");
var firstDiv = document.createElement('div');
firstDiv.contentEditable = 'false';
firstDiv.classList.add('divTable');
var rowDiv = document.createElement('div');
rowDiv.classList.add('divRow');
rowDiv.appendChild(innerDiv1);
rowDiv.appendChild(innerDiv2);
firstDiv.appendChild(rowDiv);
var secondDiv = document.createElement('div');
secondDiv.style.minWidth = '150pt';
secondDiv.style.minHeight = '80pt';
secondDiv.contentEditable = 'true';
secondDiv.classList.add('boxed');
secondDiv.setAttribute("data-text", "Case summary");
var div = document.createElement('div');
div.contentEditable = 'false';
div.classList.add('inline');
div.classList.add('boxed');
div.appendChild(firstDiv);
div.appendChild(secondDiv);
insert(div);
}
</script>
<body id="main" spellcheck="false">
<button onclick="legalCaseBox()">legalCaseBox</button>
<div id="mainDiv" contenteditable="true"></div>
</body>
【问题讨论】:
-
People let people Live... 当 sm1 创建 fiddle 时,有人说使用 SO Code sn-p,完成后有人需要 fiddle..
-
尝试运行您的代码时出现错误:“IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 is not a valid index” (Chrome 42)
-
我收到 Uncaught IndexSizeError: Failed to execute 'getRangeAt' on 'Selection': 0 不是一个有效的索引。错误
-
@Arun 我添加了焦点功能,现在试试 :)
标签: javascript html css