当前位置:网站首页>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))
})

边栏推荐
- 全文检索引擎Solr系列—–全文检索基本原理
- 7-9 one way in, two ways out (25 points)
- Puzzle (016.4) domino effect
- Analysis of gene family characteristics - chromosome location analysis
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- retrofit
- 【7.3】146. LRU缓存机制
- tonybot 人形机器人 定距移动 代码编写玩法
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- 别再问自己适不适合做软件测试了
猜你喜欢

tonybot 人形机器人 查看端口并对应端口 0701

tonybot 人形機器人 紅外遙控玩法 0630

tonybot 人形机器人 定距移动 代码编写玩法

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

Tonybot humanoid robot checks the port and corresponds to port 0701

Why is this error reported when modifying records in the database

【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?

Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them

adc128s022 ADC verilog设计实现

Code writing and playing method of tonybot humanoid robot at fixed distance
随机推荐
Tonybot Humanoïde Robot Infrared Remote play 0630
Tonybot humanoid robot infrared remote control play 0630
Zzuli:1047 logarithmic table
Get permissions dynamically
Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
7-16 find the set of integers that meet the given conditions
tonybot 人形機器人 紅外遙控玩法 0630
动态获取权限
Creation of data table of Doris' learning notes
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
一文了解微分段应用场景与实现机制
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
Mysql多表查询 #子查询
String substitution
Luogu p5536 [xr-3] core city solution
x86汇编语言-从实模式到保护模式 笔记
Zzuli:1042 sum of sequence 3
adc128s022 ADC verilog设计实现
Code writing and playing method of tonybot humanoid robot at fixed distance
String sort