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

边栏推荐
猜你喜欢

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

Accelerating strategy learning using parallel differentiable simulation

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

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

Tonybot humanoid robot checks the port and corresponds to port 0701

tonybot 人形机器人 首次开机 0630

556. The next larger element III

使用并行可微模拟加速策略学习

Analysis of gene family characteristics - chromosome location analysis

Niuke: crossing the river
随机推荐
7-18 finding the single root of polynomial by dichotomy
Recent learning summary
7-1 positive integer a+b (15 points)
7-9 one way in, two ways out (25 points)
ConstraintLayout 的使用
Code writing and playing method of tonybot humanoid robot at fixed distance
Sword finger offer 28 Symmetric binary tree
7-2 and then what time (15 minutes)
Zzuli:1048 factorial table
puzzle(016.4)多米诺效应
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Zzuli:1044 failure rate
adc128s022 ADC verilog设计实现
分布式事务(Seata) 四大模式详解
Luogu p3065 [usaco12dec]first! G problem solution
Analysis of gene family characteristics - chromosome location analysis
7-6 mixed type data format input
Solr series of full-text search engines - basic principles of full-text search
NPM install is stuck with various strange errors of node NPY
retrofit