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

}

相关推荐
工一木子28 分钟前
URL时间戳参数深度解析:缓存破坏与前端优化的前世今生
前端·缓存
半点寒12W2 小时前
微信小程序实现路由拦截的方法
前端
某公司摸鱼前端3 小时前
uniapp socket 封装 (可拿去直接用)
前端·javascript·websocket·uni-app
要加油哦~3 小时前
vue | 插件 | 移动文件的插件 —— move-file-cli 插件 的安装与使用
前端·javascript·vue.js
小林学习编程3 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
柳鲲鹏3 小时前
WINDOWS最快布署WEB服务器:apache2
服务器·前端·windows
weixin-a153003083164 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
ai小鬼头4 小时前
AIStarter最新版怎么卸载AI项目?一键删除操作指南(附路径设置技巧)
前端·后端·github
wen's5 小时前
React Native 0.79.4 中 [RCTView setColor:] 崩溃问题完整解决方案
javascript·react native·react.js
一只叫煤球的猫5 小时前
普通程序员,从开发到管理岗,为什么我越升职越痛苦?
前端·后端·全栈