js 处理编译器html 包含img的标签并设置width

var imgElements = document.getElementsByTagName('img');

for (let imgElement of imgElements) {

//1.如果有style属性,去掉style属性中的width属性和height属性

if (imgElement.hasAttribute('style')) {

// 获取style属性的值

var styleValue = imgElement.getAttribute('style');

// 使用正则表达式去掉width和height属性

var updatedStyleValue = styleValue.replace(/(width\s*:\s*\d+\s*px\s*;?|height\s*:\s*\d+\s*px\s*;?)/gi, '');

// 更新img标签的style属性

imgElement.setAttribute('style', updatedStyleValue);

}

//2.如果有height属性,去掉img中的height属性

if (imgElement.hasAttribute('height')) {

// 去掉height属性

imgElement.removeAttribute('height');

}

//3.设置img中的width属性

imgElement.setAttribute('width','100%')

}

相关推荐
老坛0019 分钟前
2025决策延迟的椭圆算子分析:锐减协同工具的谱间隙优化
前端
老坛00110 分钟前
从记录到预测:2025新一代预算工具如何通过AI实现前瞻性资金管理
前端
今禾13 分钟前
" 当Base64遇上Blob,图像转换不再神秘,让你的网页瞬间变身魔法画布! "
前端·数据可视化
华科云商xiao徐17 分钟前
高性能小型爬虫语言与代码示例
前端·爬虫
十盒半价18 分钟前
深入理解 React useEffect:从基础到实战的全攻略
前端·react.js·trae
攀登的牵牛花19 分钟前
Electron+Vue+Python全栈项目打包实战指南
前端·electron·全栈
iccb101319 分钟前
我是如何实现在线客服系统的极致稳定性与安全性的
前端·javascript·后端
一大树20 分钟前
Vue3祖孙组件通信方法总结
前端·vue.js
不要进入那温驯的良夜21 分钟前
跨平台UI自动化-Appium
前端
海底火旺22 分钟前
以一个简单的React应用理解数据绑定的重要性
前端·css·react.js