当前位置:网站首页>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-03 15:40:00 【Do front-end Pinellia ternata】
*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 !
This paper is written by mdnice Multi platform Publishing
边栏推荐
- [combinatorics] combinatorial identities (recursive combinatorial identities | sum of variable terms | simple combinatorial identities and | sum of variable terms | staggered sums of combinatorial ide
- Visual upper system design and development (Halcon WinForm) -2 Global variable design
- Seckill system 2 redis solves the problem of distributed session
- 《微服务设计》读书笔记(上)
- Distributed task scheduling XXL job
- Reading notes of "micro service design" (Part 2)
- Leasing cases of the implementation of the new regulations on the rental of jointly owned houses in Beijing
- 软件安装信息、系统服务在注册表中的位置
- qt使用QZxing生成二维码
- Microservice - fuse hystrix
猜你喜欢
Detailed pointer advanced 2
Digital image processing -- popular understanding of corrosion and expansion
Introduction, use and principle of synchronized
Idea does not specify an output path for the module
视觉上位系统设计开发(halcon-winform)-4.通信管理
Unityshader - materialcapture material capture effect (Emerald axe)
Jvm-06-execution engine
Detailed explanation of string function and string function with unlimited length
详解指针进阶1
Popular understanding of ovo and ovr
随机推荐
How to use annotations such as @notnull to verify and handle global exceptions
[cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
视觉上位系统设计开发(halcon-winform)-4.通信管理
秒杀系统3-商品列表和商品详情
子类隐藏父类的同名函数
找映射关系
do{}while()的妙用
Halcon与Winform学习第一节
Markdown file titles are all reduced by one level
CString的GetBuffer和ReleaseBuffer使用说明
Approval process design
Win10 enterprise 2016 long term service activation tutorial
App移动端测试【3】ADB命令
UnityShader——MaterialCapture材质捕捉效果 (翡翠斧头)
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
The wonderful use of do{}while()
函数栈帧的创建和销毁
Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
How are integer and floating-point types stored in memory
Visual upper system design and development (Halcon WinForm) -1 Process node design