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. 20155232 2016-2017-3 《Java程序设计》第10周学习总结

    20155232 2016-2017-3 <Java程序设计>第10周学习总结 教材学习内容总结 计算机网络 路由器和交换机组成了核心的计算机网络,计算机只是这个网络上的节点以及控制等,通 ...

  2. 20155311 实验三 敏捷开发与XP实践 实验报告

    20155311 实验三 敏捷开发与XP实践 实验报告 实验内容 XP基础 xp核心工具 相关工具 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim ...

  3. Storyboarding by Scripting

    Storyboarding by Scripting In the .osu file, under [Events]:Note: underscores can be replaced with s ...

  4. [BZOJ4383][POI2015] Pustynia-[线段树+dp+拓扑排序]

    Description 给定一个长度为n的正整数序列a,每个数都在1到10^9范围内,告诉你其中s个数,并给出m条信息,每条信息包含三个数l,r,k以及接下来k个正整数,表示a[l],a[l+1],. ...

  5. SQLite3日期与时间,常见函数

    SQLite3日期与时间,常见函数 import sqlite3 #con = sqlite3.connect('example.db') con = sqlite3.connect(":m ...

  6. RHCSA day5

    4.调整逻辑卷容量 请按照以下要求调整本地逻辑卷lvm1的容量: 调整后的逻辑卷及文件系统大小为770MiB 调整后确保文件系统中已存在的内容不能被破坏 调整后的容量可能出现误差,只要在730MiB ...

  7. 基于Cocos2d-x-1.0.1的飞机大战游戏开发实例(下)

    在飞机大战游戏开发中遇到的问题和解决方法: 1.在添加菜单时,我要添加一个有背景的菜单,需要在菜单pMenu中添加一个图片精灵,结果编译过了但是运行出错,如下图: 查了很多资料,调试了很长时间,整个人 ...

  8. python基本数据类型2

    python_day_4 今日大纲: 1. list(增删改查) 列表可以装大量的数据. 不限制数据类型. 表示方式:[] 方括号中的每一项用逗号隔开 列表和字符串一样.也有索引和切片 常用的功能: ...

  9. TCP/IP 网路基础

    一.引子         TCP/IP是"Transmission Control Protocol/Internet Protocol"的简写,翻译成中文为传输控制协议/互联网网 ...

  10. Centos下安装并设置nginx开机自启动

    一.在centos环境下安装下载并安装nginx,由于nginx需要依赖一些环境才能安装,主要依赖g++.gcc.openssl-devel.pcre-devel和zlib-devel这些环境,首先得 ...