繁体   English   中英

jQuery到纯JS

[英]Jquery to pure JS

我在jquery中有一些脚本,但是我有一个问题,在页面上我不使用Jquery,这只是我需要的示例,它要复杂得多。

$('img').each(function(){ 
      $(this).removeAttr('width');
      $(this).removeAttr('height');
      $(this).addClass('img-responsive');
    });

它用于从图像中移动属性并响应添加类,因为用户使用大量TinyMce,并且默认情况下会放置witdh和height。 我知道也许TinyMce有一些插件,但是我需要一些通用的解决方案

这应该可以,但是您可能需要为所有浏览器进行修改:

document.querySelectorAll('img').forEach(function (e) {
  e.removeAttribute('width')
  e.removeAttribute('height')
  e.classList.add('img-responsive')
})

请参阅文档以获取兼容性:

var images = document.querySelectorAll("img");
for (var i = 0; i < images.length; i++) {
    images[i].removeAttribute("width");
    images[i].removeAttribute("height");
    images[i].classList.add("img-responsive");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM