Home > Web Front-end > JS Tutorial > body text

The content selected in the js operation input box is compatible with IE and other mainstream browsers_javascript skills

WBOY
Release: 2016-05-16 16:51:30
Original
1062 people have browsed it

At work, I need to add a hyperlink to the selected content in the input box

Copy the code The code is as follows:

function addHref(des){
var selectedText="";
if(window.getSelection&&des != undefined){//Compatible with non-IE browsers, because non-IE browsers require elements for a given operation Only ID can get the selected content in the input element, so you need to enter the 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("Please select the text to add a link to!");
return;
}
var hyperlinks=prompt("Hyperlink address:","");
if(hyperlinks !=null){
var replaceString="" selectedText "";
tmpStr=textField.value;
textField.value=tmpStr.substring(0,selectionStart) replaceString tmpStr. substring(selectionEnd,tmpStr.length);
}
}
else if((document.selection)&&(document.selection.type == "Text")){//ID is not required in IE
var range=document.selection.createRange();
var formerElement=range.parentElement();
if(formerElement.tagName!="TEXTAREA"){
alert("Please specify Position the text where you want to add a hyperlink! ");
return;
}
selectedText=range.text;
var hyperlinks=prompt("Hyperlink address: ","");
if(hyperlinks!=null) {
range.text="" selectedText "< /font>";
}
}
else{
alert("Please select the text to add a link to!");
return;
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template