当前位置:网站首页>表单文本框的使用(一) 选择文本
表单文本框的使用(一) 选择文本
2022-07-03 14:31:00 【赤蓝紫】
表单文本框的使用(一) 选择文本
小技能
通过 document.forms
来获取所有的表单元素通过 form.elements
来获取表单的所有表单元素表单有用的属性 tabIndex
:数值,表示该表单字段在按Tab
键时的切换顺序,默认顺序是从小到大
<form action="">
<input type="text" tabindex="1">
<input type="text" tabindex="3">
<input type="text" tabindex="2">
</form>

文本框
文本框有两种:
input
:单行文本框。size
属性指定宽度,表示一次可显示的字符数(实际能显示多一点);maxlength
属性指定最多字符数;value
属性指定文本框的初始值。textarea
:多行文本框。rows
指定文本框的高度;cols
指定文本框的宽度,不支持size
属性。初始值应在<textarea>
和</textarea>
之间,使用value
指定无效。
input
和textarea
都会在value
属性保存自己的内容,可设置和读取文本框的值。在textarea
中设置value
属性无效
<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方法
文本框有一个select
方法,可以选中文本框中全部内容,在调用该方法时会自动将焦点设置到文本框。
<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()">选中多行文本框内容</button>
<script>
const txa = document.getElementsByTagName('textarea')[0]
function myclick(e) {
txa.select()
}
</script>
</body>
select事件
当选中文本框中的文本时,会触发select
事件。select
事件会在用户选择完文本后立即触发(IE老版本除外)。
<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('选中内容了')
})
</script>
</body>

取得选中文本
上面我们只是选中了文本,但是不知道选中了什么。HTML5进行了扩展,添加了两个属性selectionStart
和selectionEnd
。分别是文本选取的起点和终点。
txa.addEventListener('select', (e) => {
console.error('选中内容了')
console.log(e.target.selectionStart)
console.log(e.target.selectionEnd)
})

所以可以直接使用slice
方法得到选中文本。
txa.addEventListener('select', (e) => {
console.error('选中内容了')
console.log('选中文本: ', txa.value.slice(txa.selectionStart, txa.selectionEnd))
})

边栏推荐
- String reverse order
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Sword finger offer 28 Symmetric binary tree
- J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
- Fabric. JS document
- Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
- Luogu p5194 [usaco05dec]scales s solution
- Thread. Sleep and timeunit SECONDS. The difference between sleep
- Statistical capital consonants
- Raft agreement
猜你喜欢
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
Use of constraintlayout
Jiuyi cloud black free encryption free version source code
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
tonybot 人形机器人 查看端口并对应端口 0701
Exercise 9-1 time conversion
7-8 overspeed judgment
Exercise 10-1 calculate the sum of 1 to n using recursive functions
Exercise 10-3 recursive implementation of exponential functions
Four data flows and cases of grpc
随机推荐
Exercise 6-1 classify and count the number of characters
Sendmail无法发送邮件及发送过慢解决
洛谷P4047 [JSOI2010]部落划分 题解
Strategy, tactics (and OKR)
一文了解微分段应用场景与实现机制
Exercise 8-7 string sorting
7-19 check denomination (solve binary linear equation)
7-2 and then what time (15 minutes)
GRPC的四种数据流以及案例
LNMP环境mail函数不能发送邮件解决
X86 assembly language - Notes from real mode to protected mode
Add ZABBIX calculation type itemcalculated items
Niuke: crossing the river
Zabbix添加Calculated items后保存页面成空白
How Facebook moves instagram from AWS to its own server
Use of constraintlayout
Convert string to decimal integer
论文分享:Generating Playful Palettes from Images
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
JVM garbage collector