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%')

}

相关推荐
D***t13113 分钟前
前端微服务案例
前端
哀木26 分钟前
诶,这么好用的 mock 你怎么不早说
前端
Lear41 分钟前
UniApp PDF文件下载与预览功能完整实现指南
前端
Heo43 分钟前
关于XSS和CSRF,面试官更喜欢这样的回答!
前端·javascript·面试
7***A4431 小时前
Vue自然语言处理应用
前端·vue.js·自然语言处理
徐小夕1 小时前
耗时一周,我把可视化+零代码+AI融入到了CRM系统,使用体验超酷!
javascript·vue.js·github
高阳言编程1 小时前
vue2 + node + express + MySQL 5.7 的购物系统
前端
5***a9751 小时前
React Native性能优化技巧
javascript·react native·react.js
y***54882 小时前
React依赖
前端·react.js·前端框架
2***B4492 小时前
React测试
前端·react.js·前端框架