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

边栏推荐
- The mail function of LNMP environment cannot send mail
- Too many files with unapproved license
- tonybot 人形机器人 首次开机 0630
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- Exercise 6-2 using functions to sum special A-string sequences
- Luogu p4047 [jsoi2010] tribal division solution
- ShowMeBug入驻腾讯会议,开启专业级技术面试时代
- 别再问自己适不适合做软件测试了
- Exercise 10-3 recursive implementation of exponential functions
- Strategy, tactics (and OKR)
猜你喜欢

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

Exercise 9-3 plane vector addition

Exercise 8-7 string sorting

Exercise 10-1 calculate the sum of 1 to n using recursive functions

Exercise 6-2 using functions to sum special A-string sequences

必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿

牛客网:过河卒

Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue

Programming language: the essence of type system

Leetcode (4) -- find the median of two positively ordered arrays
随机推荐
Puzzle (016.4) domino effect
etcd集群权限管理和账号密码使用
Exercise 8-2 calculate the sum and difference of two numbers
Preliminary summary of structure
puzzle(016.4)多米诺效应
fpga阻塞赋值和非阻塞赋值
pyQt界面制作(登录+跳转页面)
Timecho of Tianmou technology completed an angel round financing of nearly 100 million yuan to create a native timing database of the industrial Internet of things
Understand the application scenario and implementation mechanism of differential segment
PCB中常用快捷键
2021年区域赛ICPC沈阳站J-Luggage Lock(代码简洁)
7-28 monkeys choose King (Joseph problem)
Solution to failure or slow downloading of electron when electron uses electron builder to package
Accelerating strategy learning using parallel differentiable simulation
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
编程语言:类型系统的本质
中国锂电池电解液行业市场专项调研报告(2022版)
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
MongoDB数据库入门的常用命令