当前位置:网站首页>About text selection in web pages and counting the length of selected text
About text selection in web pages and counting the length of selected text
2022-07-02 14:51:00 【still】
*Hello everyone , I am Pinellia ternata , A sand carving programmer who just started writing . If you like my article , Can pay attention to give the thumbs-up Add me WeChat :「frontendpicker」, Learn to communicate together , Become a better Engineer ~ Official account :** Pinellia ternata **, Learn more about the front end ! Am I Explore the new world !
*
*Link to the original text ==>http://sylblog.xin/archives/100
*
Preface
I opened the code I had knocked down in the past , This damn soulless code , Every function that's crooked says “ Copy and paste ” Four words . I can't sleep , After a close look at midnight , To see the words in the seam , Two words are written all over the screen “ Vegetable dog ”!!!
—— Lu xun ( false )
As an orientation CV Of CSS+JS The programmer , What I do most often , Just assign and paste , I learned something before . There is no systematic learning .CSS There are probably two attributes about text selection in ! One **::selection** , the other one 「user-select」
::selection
::selction It is used to emphasize the content selected by the user by clicking or dragging the mouse .
It's easy to use :
p::selection {
color: rgba(255, 255, 0, 1);
background-color: rgba(128, 128, 192, 1);
}

at present ::selection Only the following properties are supported :
color
background-color
text-decoration
And its related propertiestext-shadow
stroke-color
,fill-color
andstroke-width
Although the supported properties are , However, we can use these attributes to make some interesting choices .
Example - Selected emphasis
Mainly used text-shadow Shadow the text , When the text is selected , Show effect .
p::selection {
color: rgba(255, 255, 0, 1);
background-color: rgba(128, 128, 192, 1);
text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
}

「PS: more text-shadow Produced text effect , You can see :https://juejin.cn/post/7028956229925863455」
user-select
user-select It mainly controls the selection of users , For example, text cannot be selected , Partial selection, etc !
The main attribute values are :
none, auto, text, contain, all
「 For specific usage, you can see :https://juejin.cn/post/7022939685978636302」
run into sb. pointer-events:none;
When the element is set pointer-events:none, Causes the element to never become a mouse event target. I thought at first , Even when the element is set user-select Is selectable , as long as pointer-events:none; In the end, you still can't select . But the result is the opposite :
The following code , Setting elements can all be selected , however pointer-events:none;. The end result of the experiment is , Text can be selected .
p::selection {
pointer-events: none;
user-select: all;
}
The element will never become a mouse event target. however , When its descendant element pointer-events
Property specifies a different value , Mouse events can point to descendant elements , under these circumstances , The mouse event will trigger the event listener of the parent element during the capture or bubble phase .
JS Real time statistical length
js Use in .addEventListener("selectionchange"), To listen to the event selected by the text .window.getSelection().toString() Used to get the selected text .
Take the following example , When text is selected , The length of the selected text can be displayed in real time .
<p>
</p>
<p id="showConut"></p>
<script>
const showConut = document.getElementById("showConut");
const handleSelectionChange = (event) => {
let selection = window.getSelection().toString();
showConut.textContent = `(${selection.length} character )`;
return;
};
document.addEventListener("selectionchange", handleSelectionChange);
</script>

Postscript
Man , Learning things still needs to be systematic . It's probably not possible to do scattered things . This article only summarizes my choice of text , Some knowledge learned . If there is a new usage and scene in the future , Will sort it out !
边栏推荐
- kityformula-editor 配置字号和间距
- < schematic diagram of oral arithmetic exercise machine program development> oral arithmetic exercise machine / oral arithmetic treasure / children's math treasure / children's calculator LCD LCD driv
- STM32 standard firmware library function name memory (II)
- Database connection pool and data source
- 【NOI模拟赛】伊莉斯elis(贪心,模拟)
- 记一次报错解决经历依赖重复
- Fabric. JS free drawing ellipse
- LeetCode 2320. 统计放置房子的方式数
- info [email protected] : The platform “win32“ is incompatible with this module.
- 报错:npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
猜你喜欢
[Space & single cellomics] phase 1: single cell binding space transcriptome research PDAC tumor microenvironment
由粒子加速器产生的反中子形成的白洞
途家木鸟美团夏日折扣对垒,门槛低就一定香吗?
复用和分用
Quick analysis: easy to share the Internet
Edit the formula with MathType, and set it to include only mathjax syntax when copying and pasting
Fabric. JS zoom canvas
Fabric. JS free draw circle
What is erdma? Popular science cartoon illustration
Obsidian installs third-party plug-ins - unable to load plug-ins
随机推荐
Wechat applet uses towxml to display formula
Ad20 cannot select the solution of component packaging in PCB editor
Simple verification code generator for 51 single chip microcomputer experiment
obsidian安装第三方插件——无法加载插件
篇9:XShell免费版安装
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
MathML to latex
4. Array pointer and pointer array
taobao. trade. memo. Add (add remarks to a transaction) interface, Taobao store flag insertion interface, Taobao order flag insertion API interface, oauth2.0 interface
Chapter 9: xshell free version installation
【apipost】使用教程
由粒子加速器产生的反中子形成的白洞
4、数组指针和指针数组
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
字符串匹配问题
A white hole formed by antineutrons produced by particle accelerators
Fabric. JS manual bold text iText
【题解】Educational Codeforces Round 82
mongodb的认识
【NOI模拟赛】刮痧(动态规划)