问下 JS 怎么获取文本框中选中的文字? 我想获取文本框中选中的文字 ,然后设置颜色,改变字体等操作 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主想要的是html在线编辑器吧?百度:html编辑器 刚百度了一个。楼主试试<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title></head><body><form id="form"> <fieldset> <legend>测试</legend> <textarea name="text" rows="10" cols="30">absdbsd反反复复sdfjkhsdkjfhewkjhrdsfgdfg </textarea> <br /> <input name="getText" type="button" value="获取选中文本" /> <input name="getSel" type="button" value="获取光波位置" /> <br /><input name="setText" type="button" value="设置选中文本" /> <input name="setSel" type="button" value="设置光标位置" /> </fieldset></form><script type="text/javascript">Selection = function(input){ this.isTA = (this.input = input).nodeName.toLowerCase() == "textarea";};with({o: Selection.prototype}){ o.setCaret = function(start, end){ var o = this.input; if(Selection.isStandard) o.setSelectionRange(start, end); else if(Selection.isSupported){ var t = this.input.createTextRange(); end -= start + o.value.slice(start + 1, end).split("\n").length - 1; start -= o.value.slice(0, start).split("\n").length - 1; t.move("character", start), t.moveEnd("character", end), t.select(); } }; o.getCaret = function(){ var o = this.input, d = document; if(Selection.isStandard) return {start: o.selectionStart, end: o.selectionEnd}; else if(Selection.isSupported){ var s = (this.input.focus(), d.selection.createRange()), r, start, end, value; if(s.parentElement() != o) return {start: 0, end: 0}; if(this.isTA ? (r = s.duplicate()).moveToElementText(o) : r = o.createTextRange(), !this.isTA) return r.setEndPoint("EndToStart", s), {start: r.text.length, end: r.text.length + s.text.length}; for(var $ = "[###]"; (value = o.value).indexOf($) + 1; $ += $); r.setEndPoint("StartToEnd", s), r.text = $ + r.text, end = o.value.indexOf($); s.text = $, start = o.value.indexOf($); if(d.execCommand && d.queryCommandSupported("Undo")) for(r = 3; --r; d.execCommand("Undo")); return o.value = value, this.setCaret(start, end), {start: start, end: end}; } return {start: 0, end: 0}; }; o.getText = function(){ var o = this.getCaret(); return this.input.value.slice(o.start, o.end); }; o.setText = function(text){ var o = this.getCaret(), i = this.input, s = i.value; i.value = s.slice(0, o.start) + text + s.slice(o.end); this.setCaret(o.start += text.length, o.start); }; new function(){ var d = document, o = d.createElement("input"), s = Selection; s.isStandard = "selectionStart" in o; s.isSupported = s.isStandard || (o = d.selection) && !!o.createRange(); };}var f = document.forms.form;var selection = new Selection(f.text);f.getText.onclick = function(){ alert(selection.getText()); f.text.focus();};f.setText.onclick = function(){ var s = prompt("New text:", selection.getText()); s !== null && selection.setText(s); f.text.focus();};f.getSel.onclick = function(){ var s = selection.getCaret(); alert("Start: " + s.start + "\nEnd: " + s.end); f.text.focus();};f.setSel.onclick = function(){ var s = selection.getCaret(); selection.setCaret(+prompt("开始:", s.start) || 0, +prompt("结束:", s.end) || 0); f.text.focus();};</script></body></html> http://topic.csdn.net/u/20080908/09/bb568ba8-f4c9-442f-95bc-0da44fd14322.html http://topic.csdn.net/t/20020924/08/1047712.html var text=$("#id").val(); 貌似就折磨简单、、 document.selection is undefinedLine 26firefox 调式var d=document.selection.createRange().text; 这句怎么不行啊,照着别人写的啊 获取文本框中选中 的字体,然后在文本框中改变字体颜色???这能行么?在文本框中能改变font么??? 怎样判断html element 是否是隐藏的 执行js脚本时出现错误 提示"a1"未定义 thead的问题 鼠标点击的位置弹出div筐有脚本错误,大家帮看一下? 如何获得form引用 在jquery中这两种写法有什么不一样? 求救~~谢谢了~~有谁会~ 邪了门了!我也遇到了showModalDialog返回值undifined的情况! 散分100! 服务端的JAVASCRIPT与客户端的JAVASCRIPT在超文本里的写法有什么区别 多个select下拉框,选中当前某一项,其他下拉框去掉选中的值 各位大侠帮助呀,在线等 showModalDialog关闭问题
楼主试试<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form id="form">
<fieldset>
<legend>测试</legend>
<textarea name="text" rows="10" cols="30">absdbsd反反复复sdfjkhsdkjfhewkjhrdsfgdfg </textarea>
<br />
<input name="getText" type="button" value="获取选中文本" />
<input name="getSel" type="button" value="获取光波位置" />
<br /><input name="setText" type="button" value="设置选中文本" />
<input name="setSel" type="button" value="设置光标位置" />
</fieldset>
</form><script type="text/javascript">Selection = function(input){
this.isTA = (this.input = input).nodeName.toLowerCase() == "textarea";
};
with({o: Selection.prototype}){
o.setCaret = function(start, end){
var o = this.input;
if(Selection.isStandard)
o.setSelectionRange(start, end);
else if(Selection.isSupported){
var t = this.input.createTextRange();
end -= start + o.value.slice(start + 1, end).split("\n").length - 1;
start -= o.value.slice(0, start).split("\n").length - 1;
t.move("character", start), t.moveEnd("character", end), t.select();
}
};
o.getCaret = function(){
var o = this.input, d = document;
if(Selection.isStandard)
return {start: o.selectionStart, end: o.selectionEnd};
else if(Selection.isSupported){
var s = (this.input.focus(), d.selection.createRange()), r, start, end, value;
if(s.parentElement() != o)
return {start: 0, end: 0};
if(this.isTA ? (r = s.duplicate()).moveToElementText(o) : r = o.createTextRange(), !this.isTA)
return r.setEndPoint("EndToStart", s), {start: r.text.length, end: r.text.length + s.text.length};
for(var $ = "[###]"; (value = o.value).indexOf($) + 1; $ += $);
r.setEndPoint("StartToEnd", s), r.text = $ + r.text, end = o.value.indexOf($);
s.text = $, start = o.value.indexOf($);
if(d.execCommand && d.queryCommandSupported("Undo"))
for(r = 3; --r; d.execCommand("Undo"));
return o.value = value, this.setCaret(start, end), {start: start, end: end};
}
return {start: 0, end: 0};
};
o.getText = function(){
var o = this.getCaret();
return this.input.value.slice(o.start, o.end);
};
o.setText = function(text){
var o = this.getCaret(), i = this.input, s = i.value;
i.value = s.slice(0, o.start) + text + s.slice(o.end);
this.setCaret(o.start += text.length, o.start);
};
new function(){
var d = document, o = d.createElement("input"), s = Selection;
s.isStandard = "selectionStart" in o;
s.isSupported = s.isStandard || (o = d.selection) && !!o.createRange();
};
}var f = document.forms.form;
var selection = new Selection(f.text);f.getText.onclick = function(){
alert(selection.getText());
f.text.focus();
};
f.setText.onclick = function(){
var s = prompt("New text:", selection.getText());
s !== null && selection.setText(s);
f.text.focus();
};
f.getSel.onclick = function(){
var s = selection.getCaret();
alert("Start: " + s.start + "\nEnd: " + s.end);
f.text.focus();
};
f.setSel.onclick = function(){
var s = selection.getCaret();
selection.setCaret(+prompt("开始:", s.start) || 0, +prompt("结束:", s.end) || 0);
f.text.focus();
};
</script>
</body>
</html>