javascript下兼容firefox選取textarea文本的代碼
更新時(shí)間:2008年06月10日 20:21:17 作者:
兼容firefox和ie的選取textarea文本的方法
主要用到的js代碼
function getSelectedText(){
var selectedText;
var textField=document.getElementById('inputTextarea');
if(window.getSelection) selectedText=getTextFieldSelection(textField);//getTextFieldSelection(document.getElementById("inputTextArea"));
else selectedText=document.selection.createRange().text;
alert(selectedText);
}
function getTextFieldSelection(e){
//var oEvent=arguments.callee.caller.arguments[0];
if(e.selectionStart != undefined && e.selectionEnd != undefined)
return e.value.substring(e.selectionStart,e.selectionEnd);
else return "";
}
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
復(fù)制代碼 代碼如下:
function getSelectedText(){
var selectedText;
var textField=document.getElementById('inputTextarea');
if(window.getSelection) selectedText=getTextFieldSelection(textField);//getTextFieldSelection(document.getElementById("inputTextArea"));
else selectedText=document.selection.createRange().text;
alert(selectedText);
}
function getTextFieldSelection(e){
//var oEvent=arguments.callee.caller.arguments[0];
if(e.selectionStart != undefined && e.selectionEnd != undefined)
return e.value.substring(e.selectionStart,e.selectionEnd);
else return "";
}
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
相關(guān)文章
IE不支持option的display樣式,只能使用remove和add
想實(shí)現(xiàn)一個(gè)很簡(jiǎn)單的功能:當(dāng)選中“中介”時(shí),不顯示“求購(gòu)”與“求租”。本以為通過(guò)display:none即可實(shí)現(xiàn),結(jié)果發(fā)現(xiàn)在option元素上使用display:none在firefox中有效,在IE6、IE7、IE8中都無(wú)效。2009-12-12用Javascript讀取CheckBox數(shù)組的值的代碼(兼容IE與firefox)
為了同時(shí)支持Firefox和IE,我們常常用document.getElementById(id)方法來(lái)取得HTML對(duì)象。但是getElementById方法只能取得單個(gè)對(duì)象,而對(duì)于CheckBox數(shù)組則無(wú)能為力。2010-12-12用javascript來(lái)實(shí)現(xiàn)select標(biāo)簽的美化的代碼
用javascript來(lái)實(shí)現(xiàn)select標(biāo)簽的美化的代碼...2007-10-10Js密碼強(qiáng)度實(shí)時(shí)驗(yàn)證代碼
這款密碼強(qiáng)度實(shí)時(shí)驗(yàn)證代碼比較實(shí)用啊, 也比較簡(jiǎn)單,通過(guò)對(duì)輸入字符串的判斷,得知密碼的強(qiáng)度。2010-06-06