当前位置:网站首页>表单文本框的使用(一) 选择文本
表单文本框的使用(一) 选择文本
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))
})

边栏推荐
- 7-23 currency conversion (using array conversion)
- Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
- Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
- Protobuf and grpc
- Exercise 9-3 plane vector addition
- Get permissions dynamically
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- Ultra simple mobile map development
- 论文分享:Generating Playful Palettes from Images
- Mysql多表查询 #子查询
猜你喜欢
Eight sorts
7-15 calculation of PI
Exercise 9-3 plane vector addition
NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
puzzle(016.4)多米诺效应
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
7-9 find a small ball with a balance
Understand the application scenario and implementation mechanism of differential segment
Exercise 6-2 using functions to sum special A-string sequences
洛谷P4047 [JSOI2010]部落划分 题解
随机推荐
DDK for XP
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
etcd集群权限管理和账号密码使用
npm install卡住与node-npy的各种奇怪报错
Get permissions dynamically
Too many files with unapproved license
Leetcode (4) -- find the median of two positively ordered arrays
洛谷P5194 [USACO05DEC]Scales S 题解
Solution to failure or slow downloading of electron when electron uses electron builder to package
Puzzle (016.3) is inextricably linked
别再问自己适不适合做软件测试了
Convert string to decimal integer
String substitution
Find specified characters
JVM garbage collector
分布式事务(Seata) 四大模式详解
Accelerating strategy learning using parallel differentiable simulation
7-17 crawling worms (break exercise)
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars