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

}

相关推荐
陈随易3 小时前
在 VSCode 里,把项目一键部署到服务器
前端·后端·程序员
Highcharts.js3 小时前
教程:基于 React + Highcharts 构建一个单页应用程序、按需数据拉取与图表渲染
开发语言·前端·数据结构·react.js·前端框架·highcharts·页面应用
To_OC4 小时前
我被 useState 坑了两次之后,终于把它的脾气摸透了
前端·javascript·react.js
鱼樱前端4 小时前
我用 Claude Code 后,编码效率翻 3 倍。但更值钱的是别的。
前端·后端·ai编程
_lucas4 小时前
给知识库网站接入AI问答
前端·ai编程·全栈
前端糕手5 小时前
前端面试题大全:JavaScript + Vue3 + React + TypeScript + 工程化 + 性能优化
前端
我不叫武6 小时前
一个用 Rust 写的离线编码查询桌面工具
前端
Web4Browser7 小时前
指纹浏览器 API 自动化怎么接:启动 Profile、获取 CDP 端点并连接自动化框架
前端·网络·typescript·自动化
Patrick_Wilson7 小时前
从 React 到 Flutter:写给前端的一张跨端知识地图
前端·flutter·react.js
颜酱7 小时前
06 | 把 meta_config 同步进 MySQL(生成阶段)
前端·人工智能·后端