js get selected text

https://stackoverflow.com/questions/3170648/how-to-get-javascript-select-boxs-selected-text

http://www.javascriptkit.com/javatutors/copytoclipboard.shtml

    const getSelectionText = () => {
let selectedText = ``;
if (window.getSelection){
// all modern browsers and IE9+
let selection = window.getSelection();
console.log(`selection =`, selection);
selectedText = window.getSelection().toString();
console.log(`you selected text =`, selectedText);
let selectedTextWithTrim = window.getSelection().toString().trim();
console.log(`you selected text with trim() =`, selectedTextWithTrim);
}
return selectedText;
};
// getSelectionText();


<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="author" content="xgqfrms">
<meta name="generator" content="VS code">
<title></title>
</head>
<body>
<section>
<h1></h1>
<a href="https://feiqa.xgqfrms.xyz/index.html"></a>
</section>
<!-- js -->
<script>
const getSelectionText = () => {
let selectedText = ``;
if (window.getSelection){
// all modern browsers and IE9+
let selection = window.getSelection();
console.log(`selection =`, selection);
selectedText = window.getSelection().toString();
console.log(`you selected text =`, selectedText);
let selectedTextWithTrim = window.getSelection().toString().trim();
console.log(`you selected text with trim() =`, selectedTextWithTrim);
}
return selectedText;
};
// getSelectionText();
</script>
</body>
</html>

js get selected text的更多相关文章

  1. js中的text(),html() ,val()的区别

    js中的text(),html() ,val()的区别 text(),html() ,val()三个方法用于html元素的存值和取值,但是他们各有特点,text()用于html元素文本内容的存取,ht ...

  2. jQuery get selected text from SELECT (or DROPDOWN) list box

    Most of the time in JavaScript we want to do following things with Select (or dropdown) list box. – ...

  3. WordLight: highlights all occurrences of a selected text for VS2008

    https://visualstudiogallery.msdn.microsoft.com/ad686131-47d4-4c13-ada2-5b1a9019fb6f About This is a ...

  4. Selenium Webdriver——JS处理rich text(富文本框)

    126邮件正文邮件的rich text 先让selenium切换到iframe中 driver.switchTo().frame(driver.findElement(By.className(&qu ...

  5. Js script type="text/template"的使用简单说明

    <script type="text/template" id="treeTableTpl"> <tr id="{{row.id}} ...

  6. 微信小游戏 Three.js UI 2D text 简单方案

    在微信小游戏中使用 THREE.js 引擎,没有合适的 UI 库可用,只能自己动手.图片啥的都还好,text 不好弄.text 要计算 width 和 height,不然事件响应范围不对. funct ...

  7. js控制input text字符键入/字符长度限制/字母自动大写

    功能: 1.仅允许指定字符键入 2.限制长度 实现代码: <input type="text" style="width: 6em" name=" ...

  8. JS Radio结合TEXT

    <script> function fun_a(value){ if(value === "on"){ document.getElementById('a').dis ...

  9. Copy Selected Text from any window

    https://social.msdn.microsoft.com/Forums/windows/en-US/1dc356e6-9441-44de-9eda-247003fa6ef5/copy-sel ...

随机推荐

  1. 20155222 2016-2017-2 《Java程序设计》实验一

    实现Fibonacci数列功能,并进行测试. 源代码: import java.util.Scanner; public class fibonacci { public static void ma ...

  2. P3940 分组

    P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...

  3. (treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  4. Tp框架之命名空间

    命名空间,相当于虚拟目录 实现自动加载类的机制 初始命名空间:Library文件夹 初始命名空间下面有很多根命名空间: 1.Library里面的文件夹 2.APP的模块文件夹 在tp框架中,只有这两个 ...

  5. asp.net core 2.2 根据PC端和移动端自动显示不同视图而不改变url地址

    1.添加HttpRequest扩展方法 public static class RequestExtensions { //regex from http://detectmobilebrowsers ...

  6. 【python 3.6】python读取json数据存入MySQL(二)

    在网上找到一个包含全国各省市经纬度的json文件,也可以通过上次的办法,解析json关键字,构造SQL语句,插入数据库. JSON文件格式如下: [ { "name": " ...

  7. gets函数的完美替代

    众所周知 在C语言中scanf用来读取一行字符串时遇到空格或回车会停止 而若要读入一行带空格的字符串时 有些人会用gets来代替 然而,gets的最大问题在于:会读取超过数组长度上限个字符,而超出长度 ...

  8. conda环境管理

    查看环境 conda env list 创建环境 conda create -n python36 python=3.6 进入环境 source activate python36 activate ...

  9. 微软职位内部推荐-Senior Software Lead-Index Gen

    微软近期Open的职位: Position: Senior Software Development Lead Bing Index Generation team is hiring! As one ...

  10. 团队介绍 you i

    我们团队一共四个人,我们足够了解对方的优缺点,能够很好的进行交流沟通.对于一些问题也能有好的方法去解决,我做事情比较讲究高效和尽可能的完美,或者说要做到我自己觉得完美,才会停下来.对于一件事情,我有自 ...