Using JavaScript to get the selected text on the page

One of the tips introduced here is how to use JavaScript to get the selected text on the page. The most critical JavaScript API is:

event.selection = window.getSelection();

The selection here is actually an object, but if we use. ToString () or force it into a string, we will get the selected text.

$(document).ready(function () {
			$(".contenttext").mouseup(function (e) {
				var txt;
				var parentOffset = $(this).offset();
				var x = e.pageX - parentOffset.left;
				var y = e.pageY - parentOffset.top;
				txt = window.getSelection();
				if (txt.toString().length > 1) {
					alert(txt);
				}
			});
		});

If we put this code in the following page:

<html>
			<head>
				<title>Get selected text with JavaScript</title>
				<meta charset="UTF-8">
				<meta name="viewport" content="width=device-width, initial-scale=1.0">
				<script src="http://www.webhek.com/wordpress/wp-includes/js/jquery/jquery.js" type="text/javascript"></script>
				
			</head>
			<body>
				<div class="contenttext">
					和客户端的 JavaScript 不同的是,PHP 代码是运行在服务端的。如果您在您的服务器上建立了如上例类似的代码,则在运行该脚本后,客户端就能接收到其结果,但他们无法得知其背后的代码是如何运作的。您甚至可以将 WEB 服务器设置成让 PHP 来处理所有的 HTML 文件,这么一来,用户就无法得知服务端到底做了什么。 

					使用 PHP 的一大好处是它对于初学者来说及其的简单,同时也给专业的程序员提供了各种高级的特性。当您看到 PHP 长长的特性列表时,请不要害怕。您可以很快的入门,只需几个小时您就可以自己写一些简单的脚本。 
				</div>
			</body>
		</html>

When you select part of the text in the page with the mouse, you will get the selected content at the same time. I use the alert () method to display it here.

Watch the demo

Related Article
WordPress 3D rotating tag cloud

This 3D tag cloud is not the flash version of WP cumulus n years ago. This 3D rotating tag cloud Read more

Using wampserver tool to build local PHP + MySQL environment

No matter whether we use domestic space or foreign hosts, we prefer to go online after local debugging before setting Read more

How to install Linux system on Android phone

In our daily use of Android mobile phones, the installation of a complete Linux system will make mobile phones become Read more

How to backup your website data with Android phone

No matter what you do, backup is very necessary and important. Backup is just in case. Most of the time, Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image