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

}

相关推荐
han_hanker3 分钟前
下拉模糊搜索多选, 编辑,详情问题
开发语言·javascript·ecmascript
yqcoder4 分钟前
[特殊字符] Vue 3 中 Keep-Alive 对生命周期的影响:深度解析
前端·javascript·vue.js
jiayong2310 分钟前
第 33 课:任务看板视图(按状态分列)与本地持久化
开发语言·前端·javascript·学习
GISer_Jing23 分钟前
Dify可视化编排:技术架构与实战指南
前端·人工智能·ai编程
宇宙realman_99930 分钟前
DSP28335-FlashAPI使用
linux·前端·python
踩着两条虫1 小时前
VTJ 平台六大设计模式落地实战指南
开发语言·前端·人工智能·低代码·设计模式·重构·架构
Yeats_Liao1 小时前
后台 Sidebar 伸缩交互(PC + 移动端)实现
前端·javascript·css·html5
MXN_小南学前端1 小时前
computed 计算属性详解:触发时机、实战场景、Vue2 与 Vue3 对比
前端·javascript·vue.js
isNotNullX1 小时前
数据大屏怎么做?数据大屏有哪四个核心环节
开发语言·前端·javascript
E_ICEBLUE1 小时前
在 Python 中自动化转化 Markdown 为 HTML 【详细教程】
python·自动化·html