簡體   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