选择文本并使用 JavaScript 设置“文本区域”的格式
Select text and format a 'textarea' using JavaScript
我试图允许用户在注释系统(穷人的文本编辑器)中使用的文本区域中对文本进行简单的格式化。我想允许他们用光标选择文本,然后单击一个按钮来格式化它。 如何让 JavaScript 抓取并返回所选文本?
想象一下它应该是这样的:
<script>
function formatText(field) {
// Gets text
var text;
text = field.value.substring(field.selectionStart, field.selectionEnd);
// Formats it
var text = '<b'> + text + '</b>';
// Returns it
field.value.substring(field.selectionStart, field.selectionEnd); = text;
}
</script>
<body>
<textarea id="field"></textarea><button onclick="formatText('field')">Make bold</button>
</body>
以下代码获取所选文本,设置其格式,然后将其发送回文本区域 - 但是,它会替换文本区域中的所有文本...所以我只需要替换文本区域中的选定文本 - 而不是全部 - 我就会完成:
<head>
<script type="text/javascript">
function GetSelection () {
var selection = "";
var textarea = document.getElementById("field");
if ('selectionStart' in textarea) {
// Check whether some text is selected in the textarea
if (textarea.selectionStart != textarea.selectionEnd) {
selection = textarea.value.substring(textarea.selectionStart,
textarea.selectionEnd);
}
}
else { // Internet Explorer before version 9
// create a range from the current selection
var textRange = document.selection.createRange ();
// Check whether the selection is within the textarea
var rangeParent = textRange.parentElement ();
if (rangeParent === textarea) {
selection = textRange.text;
}
}
if (selection == "") {
alert ("No text is selected.");
}
else {
selection = "<b>" + selection + "</b>";
document.getElementById('field').value = selection;
}
}
</script>
</head>
<body>
<textarea id="field" spellcheck="false">Select some text within this field.</textarea>
<button onclick="GetSelection ()">Get the current selection</button>
</body>
我认为有一种方法可以指定 document.getElementByID.value.substr 这可能会做到这一点,但我不知道语法。
这是:一个穷人的文本编辑器。它使用应用于文本区域中文本的 SelectionStart 和 SelectionEnd 来获取所选文本。
然后,重新格式化后,它将文本区域文本从三个部分重新放在一起,在选择文本之前,所选文本和选择文本之后。它使用 document.getElementById('textareaid').value = 汇编文本将其放回文本区域。
<html>
<head>
<script>
function format () {
// Code for Internet Explorer
var textarea = document.getElementById("textarea");
if (document.selection)
{
textarea.focus();
var sel = document.selection.createRange();
// Alert the selected text in textarea
alert(sel.text);
// Finally replace the value of the selected
// text with this new replacement one
sel.text = '<b>' + sel.text + '</b>';
}
// Code for Mozilla Firefox
var textarea = document.getElementById("textarea");
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
// This is the selected text and alert it
//alert(sel);
var replace = '<b>' + sel + '</b>';
// Here we are replacing the selected text with this one
textarea.value = textarea.value.substring(0, start) + replace + textarea.value.substring(end, len);
var formatted = textarea.value;
document.getElementById('field').value = formatted;
}
</script>
</head>
<body>
<textarea id="textarea">One two three four five six seven eight</textarea><button onclick="format
()">Format selected text</button>
</body>
</html>
在您的代码中存在以下问题:
-
var text
每行必须以
;
结尾 -
<button onclick="formatText("field")">
引号开头必须以引号结尾。如果你需要引号,即,对于像"这是我要处理的文本"这样的字符串,那么里面的引号必须是单引号
onclick="formatText('field')"
-
text = field.value.substring(field.selectionStart, field.selectionEnd);
代码将字符串传递给函数,该函数没有值属性''方法。 我们希望文本字段中的文本,因此我们使用使用
document.getElementById('field')
.然后我们只需设置该元素的样式即可。
如果需要替换文本,只需将值分配给
document.getElementById('field').value = ...
下面是一个示例...
<script type="text/javascript" >
function formatText() {
// Gets text
//text = document.getElementById('field').value;
// Formats it
document.getElementById('field').style.fontWeight = "bold";
}
</script>
</head>
<body>
<textarea id="field"></textarea><button onclick="formatText()">Make bold</button>
</body>
在 http://jsfiddle.net/YcpUs/检查一下。
- onkeyup无法动态创建多个文本区域
- 具有所有样式的文本正在复制到可编辑文本区域
- 在文本区域POST后解码JSON
- 在文本区域中使用jQuery.text()保持换行符
- ng在下拉列表和文本区域提交
- 将文本插入光标所在的文本区域
- 离开页面导航后保留文本区域内容
- Javascript-在文本区域中断,但不在段落中中断
- 当选择值x时,Javascript需要在正确的位置显示文本区域,从而循环通过具有选择选项的表单
- 测试文本区域中的特定文本格式
- 文本编辑后,append函数不适用于文本区域
- 在文本输入区域中创建双向更新
- 是否覆盖网站上文本输入区域的默认移动设备键盘?(HTML/JS/jQuery)
- HTML 在客户端对输入类型文本(文本框)的内容进行编码(使用 Jquery 等)
- 如何在PDF文档中添加javascript来计算复选框的数量,并在文本框区域中输入值
- 编辑文本文本区域Jquery
- 获取没有子元素的父元素文本's文本
- Javascript动态创建span并垂直对齐文本:文本留在底部
- 基于文本框启用或禁用按钮.文本文本更改
- 验证表单替换文本文本区域