当前位置:网站首页>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 :
colorbackground-colortext-decorationAnd its related propertiestext-shadowstroke-color,fill-colorandstroke-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 !
边栏推荐
- MQ tutorial | exchange (switch)
- 为什么只会编程的程序员无法成为优秀的开发者?
- Advanced C language (learn malloc & calloc & realloc & free in simple dynamic memory management)
- LeetCode_字符串_简单_412.Fizz Buzz
- fatal: unsafe repository is owned by someone else 的解决方法
- C语言高级用法--函数指针:回调函数;转换表
- 3. Function pointers and pointer functions
- It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
- Obsidian installs third-party plug-ins - unable to load plug-ins
- 复用和分用
猜你喜欢

途家木鸟美团夏日折扣对垒,门槛低就一定香吗?

geoserver离线地图服务搭建和图层发布

使用mathtype编辑公式,复制粘贴时设置成仅包含mathjax语法的公式

Uniapp automated test learning

由粒子加速器产生的反中子形成的白洞

Advanced C language (realize simple address book)

【空间&单细胞组学】第1期:单细胞结合空间转录组研究PDAC肿瘤微环境

Fabric.js 自由绘制圆形

电脑怎么设置扬声器播放麦克风的声音
[email protected] : The platform “win32“ is incompatible with this module."/>info [email protected] : The platform “win32“ is incompatible with this module.
随机推荐
为什么只会编程的程序员无法成为优秀的开发者?
taobao.logistics.dummy.send( 无需物流发货处理 )接口,淘宝店铺发货API接口,淘宝订单发货接口,淘宝r2接口,淘宝oAu2.0接口
Socket and socket address
qml 弹窗框架,可定制
taobao.trade.get( 获取单笔交易的部分信息),淘宝店铺订单接口,淘宝oAuth2.0接口,淘宝R2接口代码对接分享
taobao. trades. sold. Get query the transaction data that the seller has sold (according to the creation time), Taobao store sales order query API interface, Taobao R2 interface, Taobao oauth2.0 trans
MQ教程 | Exchange(交换机)
taobao.trades.sold.get-查询卖家已卖出的交易数据(根据创建时间),淘宝店铺卖出订单查询API接口,淘宝R2接口,淘宝oAuth2.0交易接口代码分享
检查密码
Advanced usage of C language -- function pointer: callback function; Conversion table
taobao. trade. Get (get some information of a single transaction), Taobao store order interface, Taobao oauth2.0 interface, Taobao R2 interface code docking and sharing
Add vector formula in rich text editor (MathType for TinyMCE, visual addition)
求轮廓最大内接圆
Fabric. JS free drawing ellipse
用户隐私协议有些汉字编码不规范导致网页显示乱码,需要统一找出来处理一下
fatal: unsafe repository is owned by someone else 的解决方法
Find the maximum inscribed circle of the contour
PHP linked list creation and traversal
mongodb的认识
CTO如何帮助业务?