Tips for using JavaScript to retrieve selected text on the page

Source: Internet
Author: User

Tips for using JavaScript to retrieve selected text on the page

The tips here are how to use JavaScript to obtain the selected text on the page. The most critical JavaScript API is:

Event. selection = window. getSelection ();

HereselectionIt is actually an object, but if we use .toString()Or forcibly convert it into a string, we will get the selected text.

 
 
  1. $(document).ready(function () { 
  2.    $(".contenttext").mouseup(function (e) { 
  3.     var txt; 
  4.     var parentOffset = $(this).offset(); 
  5.     var x = e.pageX - parentOffset.left; 
  6.     var y = e.pageY - parentOffset.top; 
  7.     txt = window.getSelection(); 
  8.     if (txt.toString().length > 1) { 
  9.      alert(txt); 
  10.     } 
  11.    }); 
  12.   }); 

If we place this Code in the following page:

 
 
  1. <Html>
  2. <Head>
  3. <Title> Get selected text with JavaScript </title>
  4. <Meta charset = "UTF-8">
  5. <Meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
  6. <Script src = "http://www.webhek.com/wordpress/wp-includes/js/jquery/jquery.js" type = "text/javascript"> </script>
  7. </Head>
  8. <Body>
  9. <Div class = "contenttext">
  10. Unlike JavaScript on the client, PHP code runs on the server. If you have created code similar to the preceding example on your server, the client will receive the result after running the script, but they cannot know how the code behind it works. You can even set the WEB server to PHP to process all HTML files, so that users cannot know what the server has done.
  11.  
  12. One of the major advantages of using PHP is its simplicity for beginners. It also provides various advanced features for professional programmers. Do not be afraid when you see a long PHP feature list. You can get started quickly and write some simple scripts by yourself in just a few hours.
  13. </Div>
  14. </Body>
  15. </Html>

When you select some text on the page with the mouse, you can get the selected content.alert()Method.

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.