当前位置:网站首页>Use of form text box (I) select text
Use of form text box (I) select text
2022-07-03 14:35:00 【Red blue purple】
Use of form text boxes ( One ) Select text
Small skills
adopt document.formsTo get all the form elementsadopt form.elementsTo get all the form elements of the formUseful properties of the form tabIndex: The number , Indicates that the form field is pressingTabThe switching order when pressing the key , The default order is from small to large
<form action="">
<input type="text" tabindex="1">
<input type="text" tabindex="3">
<input type="text" tabindex="2">
</form>

The text box
There are two types of text boxes :
input: Single line text box .sizeProperty specifies the width , Indicates the number of characters that can be displayed at one time ( It can actually show a little more );maxlengthProperty specifies the maximum number of characters ;valueProperty specifies the initial value of the text box .textarea: Multi-line text box .rowsSpecifies the height of the text box ;colsSpecifies the width of the text box , I won't support itsizeattribute . The initial value should be within<textarea>and</textarea>Between , UsevalueInvalid specification .
input and textarea Will be in value Properties save their own content , You can set and read the value of the text box . stay textarea Set in value attribute invalid
<div class="input-box">
<input type="text" value="clz" size="10" maxlength="10">
</div>
<div class="txa-box">
<textarea type="textarea" rows="4" cols="8" maxlength="20">clz</textarea>
</div>
Select text
select Method
The text box has a select Method , You can select all the contents in the text box , When this method is called, the focus will be automatically set to the text box .
<body>
<div class="input-box">
<input type="text" value="clz" size="10" maxlength="10">
</div>
<div class="txa-box">
<textarea value="123" rows="4" cols="8" maxlength="20">clz</textarea>
</div>
<button onclick="myclick()"> Select the contents of the multiline text box </button>
<script>
const txa = document.getElementsByTagName('textarea')[0]
function myclick(e) {
txa.select()
}
</script>
</body>
select event
When the text in the text box is selected , Will trigger select event .select The event will be triggered immediately after the user selects the text (IE Except for the old version ).
<body>
<div class="txa-box">
<textarea value="123" rows="4" cols="8" maxlength="20">clz</textarea>
</div>
<script>
const txa = document.getElementsByTagName('textarea')[0]
txa.addEventListener('select', (e) => {
console.error(' Selected content ')
})
</script>
</body>

Get the selected text
We just selected the text above , But I don't know what I chose .HTML5 It has been extended , Added two properties selectionStart and selectionEnd. They are the starting point and ending point of text selection .
txa.addEventListener('select', (e) => {
console.error(' Selected content ')
console.log(e.target.selectionStart)
console.log(e.target.selectionEnd)
})

So you can use it directly slice Method to get the selected text .
txa.addEventListener('select', (e) => {
console.error(' Selected content ')
console.log(' Select the text : ', txa.value.slice(txa.selectionStart, txa.selectionEnd))
})

边栏推荐
- ConstraintLayout 的使用
- Tonybot humanoid robot starts for the first time 0630
- 剑指 Offer 28. 对称的二叉树
- Too many files with unapproved license
- 分布式事务(Seata) 四大模式详解
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- Jiuyi cloud black free encryption free version source code
- 基因家族特征分析 - 染色体定位分析
- Zzuli:1044 failure rate
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
猜你喜欢

x86汇编语言-从实模式到保护模式 笔记

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?

Doris学习笔记之数据表的创建

Puzzle (016.4) domino effect

Creation of data table of Doris' learning notes

Thinking about the arrangement problem in the backtracking problem (leetcode questions 46 and 47)

Jiuyi cloud black free encryption free version source code

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion

修改数据库中的记录为什么报这个错
![[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?](/img/f6/fe61c84f289f0e74a45946dac687a6.jpg)
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
随机推荐
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Sendmail无法发送邮件及发送过慢解决
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
Four data flows and cases of grpc
Sword finger offer 28 Symmetric binary tree
表单文本框的使用(一) 选择文本
适用于XP的DDK
Accelerating strategy learning using parallel differentiable simulation
一文了解微分段应用场景与实现机制
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Zzuli:1052 sum of sequence 4
添加Zabbix计算类型项目Calculated items
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
Some concepts about agile
Programming language: the essence of type system
【7.3】146. LRU缓存机制
Tonybot humanoid robot checks the port and corresponds to port 0701
Too many files with unapproved license
Adc128s022 ADC Verilog design and Implementation
7-14 sum integer segments (C language)