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(/(widths*:s*d+s*pxs*;?|heights*:s*d+s*pxs*;?)/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%’)
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。