JavaScript selects text and responds to the obtained implementation code

Source: Internet
Author: User

I don't know how to write JS, but I will search for it. Here I found something written by someone else: Copy codeThe Code is as follows: select (document, tanchu );
/* = Select [[
*
* Cross-browser selected text events
* @ Param
* Object o responds to the selected event's DOM object, required
* Function fn (sText, target, mouseP) callback function when the selected text is not empty, required
* |-@ Param
* |-SText selected text content
* |-Target element used to trigger the mouseup event
* |-Mouse coordinates when mouseP triggers the mouseup event
*/
Function select (o, fn ){
O. onmouseup = function (e ){
Var event = window. event | e;
Var target = event. srcElement? Event. srcElement: event.tar get;
If (/input | textarea/I. test (target. tagName) &/firefox/I. test (navigator. userAgent )){
// Select text in the text box of Firefox
Var staIndex = target. selectionStart;
Var endIndex = target. selectionEnd;
If (staIndex! = EndIndex ){
Var sText = target. value. substring (staIndex, endIndex );
Fn (sText, target );
}
}
Else {
// Obtain the selected text
Var sText = document. selection = undefined? Document. getSelection (). toString (): document. selection. createRange (). text;
If (sText! = ""){
// Pass the parameter to the callback function fn
Fn (sText, target );
}
}
}
}
/*] Select = */
Function tanchu (txt, tar ){
Alert ("the text belongs to the" + tar. tagName + "element, and the selected content is:" + txt );
}

Original Author see: http://momomolice.com/wordpress/archives/420.html

Appendix: only obtain the code of the selected text (the event should not be answered)Copy codeThe Code is as follows: function getSelectedText ()
{
If (window. getSelection)
{// This technique is 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 an older, simpler technique that returns a string
Return document. getSelection ();
}
Else if (document. selection)
{
// This is the IE-specific technique.
// We do not document the IE selection property or TextRange objects.
Return document. selection. createRange (). text;
}
}

After the function is run, the selected text is returned.

The original author is no longer available...

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.