通过记录onselect并每次存储开始和结束位置,我能够从textarea中获取高亮文本。然后,当我点击一个按钮时,我自己构建子字符串。难道没有更简单的方法来查询所选内容吗?
我有点期望在html5 dom中会有所有这些方法,类似于:
textarea.getSelectedStart() textarea.getSelectedEnd(); textArea.setSelected(start,end);
还有一种方法可以以编程方式取消textarea中的文本选择吗?
我正在根据下面的第一个解决方案放置代码。这样做有点奇怪的问题:
<script language=javascript>
function replaceCLOZE(code, questionType) {
var v = code.value;
var s = code.selectionStart;
var e = code.selectionEnd;
var t = v.substr(s, e-s);
var rep = "{:" + questionType + ":="+t+"}";
code.value = v.substr(0,s) + rep + v.substr(e,v.length-e+1);
}
function shortAnswer(code) {
replaceCLOZE(code, "SA");
}
function shortAnswerCaseSensitive(code) {
replaceCLOZE(code, "SAC");
}
function multipleChoice(code) {
replaceCLOZE(code, "MC");
}
在Chrome中,它可以正常工作。也许这只是Firefox中的一个错误,或者还有其他需要做的事情才能正确更新文本区域的外观?