修改readonly 属性值 document.getElementById("id").readOnly = true;

本文介绍如何使用JavaScript修改HTML元素的readonly属性值,通过实例代码演示实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

修改readonly 属性值:

document.getElementById("id").readOnly = true;
constructor(option) { this.container = document.getElementById(option.id) const topInput = document.createElement('div') topInput.class = 'display' this.container.appendChild(topInput) const input = document.createElement('input') input.id = 'input' topInput.appendChild(input) const answer = document.createElement('input') answer.id = 'answer' topInput.appendChild(answer) const buttonsDiv = document.createElement('div') buttonsDiv.className = 'buttons' const buttonRows = [ [{ text: 'c', classes: ['function'] }, { text: '7' }, { text: '4' }, { text: '1' }, { text: '%', classes: ['function'] }], [{ text: '÷', classes: ['function'] }, { text: '8' }, { text: '5' }, { text: '2' }, { text: '0' }], [{ text: '×', classes: ['function'] }, { text: '9' }, { text: '6' }, { text: '3' }, { text: '.' }], [ { text: '⌫', classes: ['operator', 'function'] }, { text: '-', classes: ['operator', 'function'] }, { text: '+', classes: ['function'] }, { text: '=', classes: ['operator', 'equal'] } ] ] buttonRows.forEach((rowConfig) => { const rowDiv = document.createElement('div') rowDiv.className = 'row' rowConfig.forEach((btnConfig) => { const button = document.createElement('button') button.className = 'button' button.textContent = btnConfig.text if (btnConfig.classes) { btnConfig.classes.forEach((cls) => { button.classList.add(cls) }) } rowDiv.appendChild(button) }) buttonsDiv.appendChild(rowDiv) }) this.container.appendChild(buttonsDiv) this.display = document.getElementById('input') this.answer = document.getElementById('answer') this.buttons = document.querySelectorAll('.button') this.buttonMap = new Map() this.lastChar = '' this.keyMappings = { Enter: '=', Backspace: '⌫', Escape: 'c', '*': '×', '/': '÷', '.': '.', '%': '%', '+': '+', '-': '-', 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9' } this.init() }设置上述代码中两个input光标自动跟随
最新发布
08-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值