制作文本编辑器时,需要对表单选择的内容进行修改。
然后就可以把选中的文字替换成[b]选中的文字[/b],送去后台把[b][/b]变成<strong>和</strong>了。
比如要加粗用户选择的一段文字。
网上查了获取选择文字的代码
function getSelect()
{
if(window.getSelection)
{
return window.getSelection();
}
else
{
return document.selection.createRange().text;
}
}
发现对表单里的内容不起作用。原来是选中表单里的内容时,其实浏览器并不认为你选中了。
加上
编辑框的表单.focus(),就可以真正让浏览器知道你选中那一段文字。
结果改为
function getSelect()
{
var content = document.getElementById("content");//文本编辑框的id为content
content.focus() //加上的
if(window.getSelection)
{
return window.getSelection();
}
else
{
return document.selection.createRange().text;
}
}
然后就可以把选中的文字替换成[b]选中的文字[/b],送去后台把[b][/b]变成<strong>和</strong>了。