JavaScript Select text and respond to acquired implementation code _javascript tips

Source: Internet
Author: User
Tags tagname
I do not how to write JS, but will search, here found some other people write good things:
Copy Code code as follows:

Select (document, Tanchu);
/*=select[[
*
* Cross-Browser Select text events
* @param
* Object o responds to the DOM object of the selected event, required
* function fn (STEXT,TARGET,MOUSEP) to check for callback functions when text is not empty required
* |-@param
* |-stext selected text content
* |-target the element that triggers the MouseUp event
* |-MOUSEP mouse coordinates when triggering MouseUp event
*/
function Select (o, FN) {
O.onmouseup = function (e) {
var event = Window.event | | E
var target = event.srcelement? Event.srcElement:event.target;
if (/input|textarea/i.test (target.tagname) &&/firefox/i.test (navigator.useragent)) {
Firefox selects text in a text box
var Staindex=target.selectionstart;
var endindex=target.selectionend;
if (Staindex!=endindex) {
var stext=target.value.substring (Staindex,endindex);
FN (Stext,target);
}
}
else{
Get selected text
var stext = Document.selection = = undefined? Document.getselection (). toString ():d ocument.selection.createRange (). text;
if (stext!= "") {
Passing parameters to the callback function fn
FN (stext, target);
}
}
}
}
/*]]select=*/
function Tanchu (Txt,tar) {
Alert ("The text belongs to the +tar.tagname+ element, and the selection is:" +txt);
}

See the original author: http://momomolice.com/wordpress/archives/420.html


Attach: Code that only gets the selected text (does not respond to the event)
Copy Code code as follows:

function Getselectedtext ()
{
if (window.getselection)
{//This technique are the most likely to be standardized.
GetSelection () returns a Selection object, which we do not document.
Return Window.getselection (). toString ();
}
else if (document.getselection)
{
This is the older, simpler technique that returns a string
return Document.getselection ();
}
else if (document.selection)
{
This is the ie-specific technique.
We Don't document the IE selection property or TextRange objects.
Return Document.selection.createRange (). text;
}
}

The selected text is returned when the function is run.

The original author can not test ...
Related Article

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.