当前位置:网站首页>setAttribute、getAttribute、removeAttribute
setAttribute、getAttribute、removeAttribute
2022-07-21 18:06:00 【梁什么鸭,】
问题:下面两者的区别
box.setAttribute("liang", "456");
box.liang = 789;
<body>
<div id="box" liang="123"></div>
</body>
<script>
const box = document.getElementById('box');
/*// 标签里元素里的自定义属性
box.setAttribute('liang', '456');
console.log(box.liang); // 输出undefined*/
/*// JS里对象的 自定义属性
box.liang = 789;
console.log(box.liang); // 输出789*/
console.log(box.liang); // 输出undefined
</script>
setAttribute 设置的是标签里的liang
box.liang 设置的是box这个对象里的属性liang
它跟标签是没有关系的,既不能修改div标签里的liang,也不能获取
输出789,输出的是box这个对象的属性liang被 box.liang这条语句设置为值 789
而不是 div标签里的liang的值 被 box.liang这条语句修改成789
栗子1:
<script>
window.onload = function () {
let inp = document.getElementById('inp');
inp.value = 'liang';
};
</script>
</head>
<body>
<input type="text" id="inp">
</body>
页面立即显示 liang
打开控制台,查看Element
发现并没有value这个属性,也就是说 输出的是 box这个对象上的 value属性值,而不是input标签里的value属性值
栗子2:
<script>
window.onload = function () {
let inp = document.getElementById('inp');
inp.value = 'liang';
/*inp.setAttribute('value', 'liang');*/
};
</script>
</head>
<body>
<div id="box" liang="123">
<input type="text" id="inp" value="123">
<p></p>
<span></span>
<img src="#" alt="">
</div>
</body>
立即输出liang
查看Element:
标签内的value 没有改变
栗子3:
/*inp.value = 'liang';*/
inp.setAttribute('value', 'liang');
页面也立即输出liang
打开控制台查看Element
发现标签里添加了value属性
栗子4:
<script>
window.onload = function () {
let inp = document.getElementById('inp');
/*inp.value = 'liang';*/
inp.setAttribute('value', 'liang');
};
</script>
</head>
<body>
<input type="text" id="inp" value="123">
</body>
页面立即输出liang
查看Element:
标签内的value属性改为了 liang
在控制台操作以下输入:
原始状态:
操作1:
无需刷新,立即输出123
操作2:
查看element,已做改变:
页面没有改变,控制台输出undefined
操作3:
无需刷新,直接显示 789
可以看出,当不刷新页面时,setAttribute输出undefined。
原因:
inp.value在修改值的过程中,修改的不是标签内 value属性的值,而是修改input里的值。
而setAttribute 修改的是 value属性的值,但不会触发视图更新
所以 只用setAttribute 操作非法的自定义属性的标签属性节点
栗子:
<script>
window.onload = function () {
let inp = document.getElementById('inp');
inp.value = 'liang';
/*inp.setAttribute('value', 'liang');*/
};
</script>
</head>
<body>
<div id="box" liang="123">
<input type="checkbox" id="inp" checked="123">
<!--不管checked 的值是什么,复选框都会被选中-->
</div>
控制台输入:
复选框默认选中,就算checked是个空值,也会是选中状态
如何让复选框不选中?
inp.checked = false;
但是 inp.setAttribute(“checked”, false); 还是选中状态
那么可以 inp.removeAttribute(‘checked’);
边栏推荐
- MySQL installation configuration -version 8.0 -windows
- PageHelper的使用
- 嵌入式之SD卡异常问题分析
- Permission
- High frequency leetcode deep search part: 98 Validate binary search tree
- 排序方法:冒泡排序(使用数组实现一串数字的顺序排列(从大到小或从小到大))
- EAS BOS report development
- EAS BOS custom export (including excel style setting, multi tab export, export file directory verification and backup)
- 26.反转链表 II
- EAS 登录界面修改
猜你喜欢
EAS BOS 自定义导出(含Excel样式设置、多页签导出、导出文件目录校验及备份)
[original] principle of an automatic nine point calibration tool (including part of the source code)
performance 优化
Development environment EAS login license modification
产品切换国产芯片步骤,STM32F4切换GD32
36.删除链表的倒数第 N 个结点
Activity启动(launchActivity/startActivity)_(1)_流程图之WMS
Responsive layout - Mobile Web pixels
EAS Web BIM Start Access prompt 500 Error
EAS BOS custom export (including excel style setting, multi tab export, export file directory verification and backup)
随机推荐
EAS 扩展报表资料大礼包(从入门到精通)
[Datasheet ] 计量芯片CS5480数据手册解读
关于线程 thread (4)线程的交互
28.接雨水
EAS 登录界面修改
鼓励企业知识共享的好处,你知道多少?
PageHelper的使用
simplest-i18n
EAS 审批流相关表
Oracle error list
EAS BOS 自定义导出(含Excel样式设置、多页签导出、导出文件目录校验及备份)
响应式布局——字体常用单位
嵌入式之SD卡异常问题分析
迅雷面试题
数组的反转(逆序输出)(定义一个数组并赋值按逆序输出这个数组)
[STM32 ]内部独立看门狗IWDG
High frequency leetcode deep search part: 112 Path sum
EAS版本升级后元数据发布报错
知识分享|分享一些提升企业文档管理水平的方法
嵌入式之网络问题总结(网卡丢包、网卡无法识别)