js操作輸入框中選擇內(nèi)容兼容IE及其他主流瀏覽器
更新時(shí)間:2014年04月22日 17:28:33 作者:
這篇文章主要介紹了js如何操作輸入框中選擇的內(nèi)容兼容IE及其他主流瀏覽器,需要的朋友可以參考下
工作中遇到需要給輸入框中選中的內(nèi)容增加超鏈接
function addHref(des){
var selectedText="";
if(window.getSelection&&des != undefined){//兼容非IE瀏覽器,由于非IE瀏覽器需要給定操作的元素ID才可以獲取輸入元素中選中的內(nèi)容,因此需要輸入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("請(qǐng)選擇需要添加鏈接的文字!");
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("請(qǐng)?jiān)谥付ㄎ恢眠x擇需要添加超鏈接的文字!");
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("請(qǐng)選擇需要添加鏈接的文字!");
return;
}
}
復(fù)制代碼 代碼如下:
function addHref(des){
var selectedText="";
if(window.getSelection&&des != undefined){//兼容非IE瀏覽器,由于非IE瀏覽器需要給定操作的元素ID才可以獲取輸入元素中選中的內(nèi)容,因此需要輸入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("請(qǐng)選擇需要添加鏈接的文字!");
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("請(qǐng)?jiān)谥付ㄎ恢眠x擇需要添加超鏈接的文字!");
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("請(qǐng)選擇需要添加鏈接的文字!");
return;
}
}
相關(guān)文章
js+html5實(shí)現(xiàn)手機(jī)九宮格密碼解鎖功能
這篇文章主要為大家詳細(xì)介紹了js+html5實(shí)現(xiàn)手機(jī)九宮格密碼解鎖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
用javascript連接access數(shù)據(jù)庫(kù)的方法
用javascript連接access數(shù)據(jù)庫(kù)的方法...2006-11-11
ECharts調(diào)用接口獲取后端數(shù)據(jù)的四種方法總結(jié)
echarts是我們經(jīng)常用到的數(shù)據(jù)可視化圖形,但是后端反饋給我們的數(shù)據(jù)經(jīng)常是數(shù)組包對(duì)象的集合類型,下面這篇文章主要給大家介紹了關(guān)于ECharts調(diào)用接口獲取后端數(shù)據(jù)的四種方法,需要的朋友可以參考下2022-11-11
父頁(yè)面iframe中的第三方子頁(yè)面跨域交互技術(shù)—postMessage實(shí)現(xiàn)方法
web網(wǎng)站通過(guò)iframe嵌入第三方web頁(yè)面,父頁(yè)面和子頁(yè)面如果需要數(shù)據(jù)交互,顯然違反了同源策略,在HTML5標(biāo)準(zhǔn)引入的window對(duì)象下的postMessage方法,可以允許來(lái)自不同源的JavaScript腳本采用異步方式進(jìn)行有限的通信,可以實(shí)現(xiàn)跨文本檔、多窗口、跨域消息傳遞...2023-06-06
js實(shí)現(xiàn)有趣的倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)有趣的倒計(jì)時(shí)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01

