原生 js 修改属性

1.setAttribute

描述:设置元素的【属性】

语法:元素名.setAttribute('属性','属性值');

例子:

box.setAttribute('style','background-color:red;height:400px');

注意:设置的是行内样式

2.getAttribute

描述:获取属性值

语法:元素名.getAttribute('属性名');

例子:

console.log(box.getAttribute('style'));//background-color:red;height:300px

注意:

只能获取行内样式

3.removeAttribute()

描述:删除一个属性

语法:元素名.removeAttribute('属性名');

例子:

box.removeAttribute('style');

4. 最古老的方式

描述 : element.属性

btn.id = 'qwe' 

 

posted @   会前端的洋  阅读(510)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示