JS operation input box to select content compatible with IE and other mainstream browser _javascript tips

Source: Internet
Author: User
You have to add a hyperlink to the selection in the input box at work
Copy Code code as follows:

function Addhref (DES) {
var selectedtext= "";
if (window.getselection&&des!= undefined) {//compatible non IE browser, because non IE browser requires the element ID of the given operation to get the selected content in the INPUT element, 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 (Select the text you want to add a link to!) ");
Return
}
var hyperlinks=prompt ("hyperlink address:", "");
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 does not require an ID
var range=document.selection.createrange ();
var formerelement=range.parentelement ();
if (formerelement.tagname!= "TEXTAREA") {
Alert (Select the text you want to add a hyperlink to at the specified location!) ");
Return
}
Selectedtext=range.text;
var hyperlinks=prompt ("hyperlink address:", "");
if (hyperlinks!=null) {
range.text= "<a href=" "+hyperlinks+" ' target= ' _blank ' ><b><u><font color= ' #686600 ' > ' + SelectedText + "</font></u></b></a>";
}
}
else{
Alert (Select the text you want to add a link to!) ");
Return
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.