js操作輸入框中選擇內容兼容IE及其他主流瀏覽器
更新時間:2014年04月22日 17:28:33 作者:
這篇文章主要介紹了js如何操作輸入框中選擇的內容兼容IE及其他主流瀏覽器,需要的朋友可以參考下
工作中遇到需要給輸入框中選中的內容增加超鏈接
function addHref(des){
var selectedText="";
if(window.getSelection&&des != undefined){//兼容非IE瀏覽器,由于非IE瀏覽器需要給定操作的元素ID才可以獲取輸入元素中選中的內容,因此需要輸入ID
var textField=document.getElementById(des);
var selectionStart=textField.selectionStart;
var selectionEnd=textField.selectionEnd;
if(selectionStart != undefined && selectionEnd != undefined){
selectedText=textField.value.substring(selectionStart,selectionEnd);
}
if(selectedText==""){
alert("請選擇需要添加鏈接的文字!");
return;
}
var hyperlinks=prompt("超鏈接地址:","");
if(hyperlinks!=null){
var replaceString="<a href='"+hyperlinks+"' target='_blank'><b><u><font color='#686600'>" + selectedText + "</font></u></b></a>";
tmpStr=textField.value;
textField.value=tmpStr.substring(0,selectionStart)+replaceString+tmpStr.substring(selectionEnd,tmpStr.length);
}
}
else if((document.selection)&&(document.selection.type == "Text")){//IE中不需要ID
var range=document.selection.createRange();
var formerElement=range.parentElement();
if(formerElement.tagName!="TEXTAREA"){
alert("請在指定位置選擇需要添加超鏈接的文字!");
return;
}
selectedText=range.text;
var hyperlinks=prompt("超鏈接地址:","");
if(hyperlinks!=null){
range.text="<a href='"+hyperlinks+"' target='_blank'><b><u><font color='#686600'>" + selectedText + "</font></u></b></a>";
}
}
else{
alert("請選擇需要添加鏈接的文字!");
return;
}
}
復制代碼 代碼如下:
function addHref(des){
var selectedText="";
if(window.getSelection&&des != undefined){//兼容非IE瀏覽器,由于非IE瀏覽器需要給定操作的元素ID才可以獲取輸入元素中選中的內容,因此需要輸入ID
var textField=document.getElementById(des);
var selectionStart=textField.selectionStart;
var selectionEnd=textField.selectionEnd;
if(selectionStart != undefined && selectionEnd != undefined){
selectedText=textField.value.substring(selectionStart,selectionEnd);
}
if(selectedText==""){
alert("請選擇需要添加鏈接的文字!");
return;
}
var hyperlinks=prompt("超鏈接地址:","");
if(hyperlinks!=null){
var replaceString="<a href='"+hyperlinks+"' target='_blank'><b><u><font color='#686600'>" + selectedText + "</font></u></b></a>";
tmpStr=textField.value;
textField.value=tmpStr.substring(0,selectionStart)+replaceString+tmpStr.substring(selectionEnd,tmpStr.length);
}
}
else if((document.selection)&&(document.selection.type == "Text")){//IE中不需要ID
var range=document.selection.createRange();
var formerElement=range.parentElement();
if(formerElement.tagName!="TEXTAREA"){
alert("請在指定位置選擇需要添加超鏈接的文字!");
return;
}
selectedText=range.text;
var hyperlinks=prompt("超鏈接地址:","");
if(hyperlinks!=null){
range.text="<a href='"+hyperlinks+"' target='_blank'><b><u><font color='#686600'>" + selectedText + "</font></u></b></a>";
}
}
else{
alert("請選擇需要添加鏈接的文字!");
return;
}
}
相關文章
用javascript連接access數(shù)據(jù)庫的方法
用javascript連接access數(shù)據(jù)庫的方法...2006-11-11
ECharts調用接口獲取后端數(shù)據(jù)的四種方法總結
echarts是我們經常用到的數(shù)據(jù)可視化圖形,但是后端反饋給我們的數(shù)據(jù)經常是數(shù)組包對象的集合類型,下面這篇文章主要給大家介紹了關于ECharts調用接口獲取后端數(shù)據(jù)的四種方法,需要的朋友可以參考下2022-11-11
父頁面iframe中的第三方子頁面跨域交互技術—postMessage實現(xiàn)方法
web網站通過iframe嵌入第三方web頁面,父頁面和子頁面如果需要數(shù)據(jù)交互,顯然違反了同源策略,在HTML5標準引入的window對象下的postMessage方法,可以允許來自不同源的JavaScript腳本采用異步方式進行有限的通信,可以實現(xiàn)跨文本檔、多窗口、跨域消息傳遞...2023-06-06

