function Test(textboxID) { try{ var selecter=window.getSelection(); if((selecter!=null&&selecter.replace(/\s/g,'')!="")&&(document.activeElement.id==textboxID)){ alert("你选中了textbox中的文字:"+selecter);} }catch(err){ var selecter=document.selection.createRange(); var s=selecter.text; if((s!=null&&s.replace(/\s/g,'')!="")&&(document.activeElement.id==textboxID)){ alert("你选中了textbox中的文字:"+s)} } } 是这样么?
我日 不要吵baidu的东西 这个明显不对
<script type="text/javascript"> function chkSel(id) { var select_field = document.getElementById(id); word=''; if (document.selection) { var sel = document.selection.createRange(); if (sel.text.length > 0) { word = sel.text; } } /*ie浏览器*/ else if (select_field.selectionStart || select_field.selectionStart == '0') { var startP = select_field.selectionStart; var endP = select_field.selectionEnd; if (startP != endP) { word = select_field.value.substring(startP,endP); } } /*标准浏览器*/ if(word=='') return false; else return true; } </script> <input type=text id=a value="abcd"> <br /> <br /> <button onclick="alert(chkSel('a'))">是否选了</button>
我怎么看不明白 谁能注释下document.selection 是什么
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>stat byte</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <textarea> keywords this is my page </textarea> <script language="javascript"> function setVal(){ var str = ""; var isIE=!!document.all; if(isIE){ var rng=document.selection.createRange(); str = rng.text; } else { var s = window.getSelection(); str = s.toString(); } alert(str + "-" + str.length); } </script> <input type="button" value="is select" onclick="setVal()"/> </body> </html>document.selection//获取选中区域,即高亮文本块
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>selectedText.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body> <div id="dvCT">中国人民共和国共和 </div> <input type="button" value="清除颜色" onclick="$('dvCT').innerHTML=removeHTML($('dvCT').innerHTML)" /> <script language="javascript"> function $(Id){return document.getElementById(Id);} function removeHTML(v){return v.replace(/<[^>]+>/g,'');} document.onmouseup=function(){ var isIE=!!document.all; if(isIE){ var rng=document.selection.createRange(); if(rng.text!='')rng.pasteHTML("<font size='22'>" + rng.text.fontcolor('#ff0000') + "</font>");//跟多就添加样式style } else { var s = window.getSelection(); if(s.toString()!=''){ var font=document.createElement('font'); font.color='#ff0000'; font.size = 22; font.innerHTML=s; s.getRangeAt(0).surroundContents(font); s.removeAllRanges(); } } } </script> </body> </html>
{
try{
var selecter=window.getSelection();
if((selecter!=null&&selecter.replace(/\s/g,'')!="")&&(document.activeElement.id==textboxID)){
alert("你选中了textbox中的文字:"+selecter);}
}catch(err){
var selecter=document.selection.createRange();
var s=selecter.text;
if((s!=null&&s.replace(/\s/g,'')!="")&&(document.activeElement.id==textboxID)){
alert("你选中了textbox中的文字:"+s)}
}
}
是这样么?
function chkSel(id)
{
var select_field = document.getElementById(id);
word='';
if (document.selection) {
var sel = document.selection.createRange();
if (sel.text.length > 0) {
word = sel.text;
}
} /*ie浏览器*/
else if (select_field.selectionStart || select_field.selectionStart == '0') {
var startP = select_field.selectionStart;
var endP = select_field.selectionEnd;
if (startP != endP) {
word = select_field.value.substring(startP,endP);
}
} /*标准浏览器*/
if(word=='') return false; else return true;
}
</script>
<input type=text id=a value="abcd">
<br />
<br />
<button onclick="alert(chkSel('a'))">是否选了</button>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>stat byte</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<textarea>
keywords this is my page
</textarea>
<script language="javascript">
function setVal(){
var str = "";
var isIE=!!document.all;
if(isIE){
var rng=document.selection.createRange();
str = rng.text;
} else {
var s = window.getSelection();
str = s.toString();
}
alert(str + "-" + str.length);
}
</script>
<input type="button" value="is select" onclick="setVal()"/>
</body>
</html>document.selection//获取选中区域,即高亮文本块
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>selectedText.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="dvCT">中国人民共和国共和
</div>
<input type="button" value="清除颜色" onclick="$('dvCT').innerHTML=removeHTML($('dvCT').innerHTML)" />
<script language="javascript">
function $(Id){return document.getElementById(Id);}
function removeHTML(v){return v.replace(/<[^>]+>/g,'');}
document.onmouseup=function(){
var isIE=!!document.all;
if(isIE){
var rng=document.selection.createRange();
if(rng.text!='')rng.pasteHTML("<font size='22'>" + rng.text.fontcolor('#ff0000') + "</font>");//跟多就添加样式style
} else {
var s = window.getSelection();
if(s.toString()!=''){
var font=document.createElement('font');
font.color='#ff0000';
font.size = 22;
font.innerHTML=s;
s.getRangeAt(0).surroundContents(font);
s.removeAllRanges();
}
}
}
</script>
</body>
</html>
JS给它一个获得焦点的函数,函数定义document.文本框对象.select();
这样在文本框里点击一下,文本框里的文字就全部选中,成了兰色背景的反色显示,表示选中了
jQuery语法是这样:
$(document).ready(function(){
$(":text").focus(function(){
$(this).select();
});
}) //这里假设是只有一个文本框