<input type="text" value="computer">
<script>
const uname = document.querySelector('input')
// 获取表单属性的值用 表单.value
// innerHTML只能获取普通元素内容,得不到表单内容
console.log(uname.value)
uname.value = 'sky'
console.log(uname.value)
</script>
注意:innerHTML只能获取普通元素内容,得不到表单内容
加上以下代码后:
uname.type = 'password'
<input type="checkbox" name="" id="">
<script>
const ipt = document.querySelector('input')
console.log(ipt.checked)
</script>
加上 ipt.checked = true
(写成ipt.checked = 'true'
也可以,实际上写什么都可以,因为有隐式转换,但是不提倡)
<input type="checkbox" name="" id="">
<script>
const ipt = document.querySelector('input')
ipt.checked = true
console.log(ipt.checked)
</script>
disabled
举例:
(注意,button是在disabled = true时禁用,默认情况下是false,不禁用)
<button>feiyun</button>
<script>
const btn = document.querySelector('button')
console.log(btn.disabled)//默认false,不禁用按钮
btn.disabled = true
console.log(btn.disabled)
</script>
data-
开头举例:
<div data-id="1" data-spm="dontknow">1</div>
<div data-id="2">2</div>
<div data-id="3">3</div>
<script>
const one = document.querySelector('div')
console.log(one.dataset)
console.log(one.dataset.id)
console.log(one.dataset.spm)
</script>