我一直在研究一个可压缩的div,我会在超过规定的最大长度时突出显示文本。
Codepen在这里: http://codepen.io/doublesidedstickytape/pen/NqBMXR
一切正常 - 除非用户按下Return键 - 然后它会中断!
我认为我可以在第一个[x]数量的字符之后获取所有HTML内容,然后循环遍历每个元素(充当新行) - 围绕它们包装高亮类在我隐藏的div。
我不知道如何实现这一目标。
HTML
<div class="wrapper">
<div contenteditable="true" data-maxlength="10" class="editable"></div>
<div contenteditable="true" class="readonly"></div>
</div>
JS
$(document).on("keyup", "div.editable", function(event) {
// GOOD TO STORE THIS IN A VAR
// PREVENTS THE BROWSER HAVING TO
// FIGURE OUT WHAT $(this) IS
// EACH TIME YOU CALL IT
var element = $(this);
// KEYUP
if (event.type == "keyup") {
var maximumLength = element.attr("data-maxlength");
var currentLength = element.text().length;
var content = element.text();
// CURRENT LENGTH IS GREATER THAN
// THE SPECIFIED MAXIMUM LENGTH
if (currentLength > maximumLength) {
var over = element.html().substr(maximumLength);
over = "<span class='highlight'>" + over + "</span>";
content = element.html().substr(0, maximumLength) + over;
}
$("div.readonly").html(content);
}
});
CSS
body {
margin: 0 auto;
font-family: Arial, Helvetica, sans-serif;
font-size: 1em;
line-height: 1.4em;
color: #444;
}
div.wrapper {
position: relative;
margin-top: 15px;
}
div.editable,
div.readonly {
width: 300px;
white-space: wrap;
position: absolute;
top: 0;
left: 0;
}
div.editable {
border-bottom: 1px solid #00aeed;
outline: none;
}
div.readonly {
z-index: -99;
color: transparent;
background: transparent;
}
span.highlight {
background: #fcc !important;
}
$(document).on("keyup", "div.editable", function(event) {
// GOOD TO STORE THIS IN A VAR
// PREVENTS THE BROWSER HAVING TO
// FIGURE OUT WHAT $(this) IS
// EACH TIME YOU CALL IT
var element = $(this);
// KEYUP
if (event.type == "keyup") {
var maximumLength = element.attr("data-maxlength");
var currentLength = element.text().length;
var content = element.text();
// CURRENT LENGTH IS GREATER THAN
// THE SPECIFIED MAXIMUM LENGTH
if (currentLength > maximumLength) {
var over = element.html().substr(maximumLength);
over = "<span class='highlight'>" + over + "</span>";
content = element.html().substr(0, maximumLength) + over;
}
$("div.readonly").html(content);
}
});
&#13;
body {
margin: 0 auto;
font-family: Arial, Helvetica, sans-serif;
font-size: 1em;
line-height: 1.4em;
color: #444;
}
div.wrapper {
position: relative;
margin-top: 15px;
}
div.editable,
div.readonly {
width: 300px;
white-space: wrap;
position: absolute;
top: 0;
left: 0;
}
div.editable {
border-bottom: 1px solid #00aeed;
outline: none;
}
div.readonly {
z-index: -99;
color: transparent;
background: transparent;
}
span.highlight {
background: #fcc !important;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="wrapper">
<div contenteditable="true" data-maxlength="10" class="editable"></div>
<div contenteditable="true" class="readonly"></div>
</div>
&#13;
答案 0 :(得分:1)
我制定了一个解决方案,请参阅:http://jsfiddle.net/alan0xd7/6o1sr5fg/
如果有需要突出显示的文字,首先我们只需将内容HTML复制到div.readonly
进行进一步处理。如果不需要突出显示,我们只需清空div
。
collectTextNodes()
函数遍历内容HTML中的所有DOM节点,并收集所有文本节点。收集的节点只包含文本,没有子节点。
对于我们收集的所有文本节点,我们通过highlight()
函数运行它们以突出显示文本。
highlight()
首先跳过maximumLength
个字符,然后将所有内容包装在<span class='highlight'>
中。