在 textarea 中编辑 HTML,jQ 显示结果(不是 OP 中的 Struts2 req)
抱歉,这是 OP O_o 中请求的 jQuery 实现 vs Struts2。希望它给 Struts2 用户一个起点?
这有一些控制按钮的格式,由 textarea 中的快捷键组合和动态显示更改的 div 支持。
<head>
<title>Text Editor (test)</title>
<script type="text/javascript" src="../lib/js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
updCont();
var ht = window.innerHeight - $("#dvInput").css("height").replace(/[^.0-9]/g, '') - 30;
$("#dvCont").css("height", ht + "px");
$("#txtCont").keydown(function(e){
var curKey = e.which ? e.which : e.key;
if(e.ctrlKey && e.shiftKey){
if([56,190,54].indexOf(curKey)>-1)
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
if(curKey == 56) fmt("bull");// *
else if(curKey == 190) fmt("indnt");// >
else if(curKey == 54) fmt("para");// ^
}else if(e.ctrlKey){
if([66,73,13].indexOf(curKey)>-1)
e.preventDefault ? e.preventDefault() : (e.returnValue = false);
if(curKey == 66) fmt("bld");// B
else if(curKey == 73) fmt("ital");// I
else if(curKey == 13) fmt("crlf");// [enter]
}
updCont();
});
$("#inp").keydown(function(e){alert(e.which)})
});
function updCont(){
$("#dvCont").html($("#txtCont").val());
}
function fmt(inAct){
var oTxt = $("#txtCont");
var s = oTxt[0].selectionStart;
var e = oTxt[0].selectionEnd;
var raVal = [oTxt.val().substring(0, s), oTxt.val().substring(s, e), oTxt.val().substring(e), s, e];
var ofstStart = 0;
var ofstEnd = 0;
if(inAct=="bld"){
oTxt.val(raVal[0] + "<b>" + raVal[1] + "</b>" + raVal[2]);
//if you don't want text selected, add raVal[1].length to ofstStart
ofstStart = 3;
//uncomment if you want cursor after closing tag (and note also ofstStart same point to unselect)
//ofstEnd = 4;
}else if(inAct=="ital"){
oTxt.val(raVal[0] + "<i>" + raVal[1] + "</i>" + raVal[2]);
ofstStart = 3;
//ofstEnd = 4;
}else if(inAct=="indnt"){
oTxt.val(raVal[0] + "<div class='dnt'>" + raVal[1] + "</div>" + raVal[2]);
ofstStart = 17;
//ofstEnd = 5;
}else if(inAct=="bull"){
oTxt.val(raVal[0] + "<br /> • " + raVal[1] + raVal[2]);
ofstStart = 20;
}else if(inAct=="para"){
oTxt.val(raVal[0] + "<p>" + raVal[1] + "</p>" + raVal[2]);
ofstStart = 3;
//ofstEnd = 4;
}else if(inAct=="href"){
oTxt.val(raVal[0] + "<a href='' class='' title=''>" + raVal[1] + "</a>" + raVal[2]);
ofstStart = 29;
//ofstEnd = 4;
}else if(inAct=="crlf"){
oTxt.val(raVal[0] + "<br />" + raVal[1] + raVal[2]);
ofstEnd = 5;
}
updCont();
oTxt[0].selectionStart = s + ofstStart;
oTxt[0].selectionEnd = s + ofstStart + raVal[1].length;
oTxt[0].focus();
}
</script>
<style>
.dnt{margin-left:40px;}
.styleBtn{width:20px;margin:2px 10px;border:1px solid black; padding:1px 7px;background-color:#EEE;float:left;text-align:center;}
</style>
</head><body>
<div id="dvInput">
<div style="border:1px solid black;border-radius:5px;background-color:silver;font-weight:bold;padding:2px 5px;float:left;">
<div style="float:left;margin:2px 20px;">Text Input:</div>
<span onclick="fmt('bld');" title="CTRL+B: Bold selected text">
<div class="styleBtn"><b>B</b></div>
</span>
<span onclick="fmt('ital');" title="CTRL+I: Italicize selected text">
<div class="styleBtn"><i>I</i></div>
</span>
<span onclick="fmt('indnt');" title="CTRL+Shift+>: <div> Indent a selected block of text\n(No remove shortcut.\nManually delete <div></div> to undo/outdent)">
<div class="styleBtn">></div>
</span>
<!-- <span onclick="fmt('outdnt');"><</span> -->
<span onclick="fmt('bull');" title="CTRL+Shift+*: Bullet (and new line)">
<div class="styleBtn">•</div>
</span>
<span onclick="fmt('para');" title="CTRL+Shift+^: Paragraph <p> around highlighted/selected text">
<div class="styleBtn">¶</div>
</span>
<span onclick="fmt('crlf');" title="CTRL+Enter: New Line <br>">
<div class="styleBtn">↲</div>
</span>
<span onclick="fmt('href');" title="CTRL+Shift+A: <a href>">
<div class="styleBtn"><a></div>
</span>
<!-- <input id="inp" size="2">-enter char, get ascii number -->
</div>
<textarea id="txtCont" rows="10" style="width:100%;">
</textarea><br />
</div>
Display:
<div id="dvCont" style="border:1px solid black;border-radius:5px;overflow-y:auto;"></div>
</body>
</html>
StackOverflow 编辑器有一些巧妙的格式,超出了我想要在我的显示中实现的范围。