A tip introduced here is how to use JavaScript to get selected text on a page. The most critical JavaScript API is:
The code copy is as follows:
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.
The code copy is as follows:
$(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 place this code on the following page:
The code copy is as follows:
<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="//www.VeVB.COM/wordpress/wp-includes/js/jquery/jquery.js" type="text/javascript"></script>
</head>
<body>
<div>
Unlike JavaScript on the client, PHP code runs on the server. If you build code similar to the example above on your server, the client will receive the result after running the script, but they can't know how the code behind it works. You can even set up the WEB server to let PHP handle all HTML files, so that users cannot know what the server is doing.
One of the benefits of using PHP is that it is very simple for beginners, and it also provides professional programmers with various advanced features. Don't be afraid when you see PHP's long list of features. You can get started quickly and in just a few hours you can write some simple scripts yourself.
</div>
</body>
</html>
When you select part of the text in the page with the mouse, you get the selected content. I use the alert() method to display it here.