多个类名 - getelementsbyclassname

时间:2018-04-04 21:59:58

标签: javascript dom

基本上我想要更改徽标,有2个当前徽​​标,分别标有类...' Header-branding-logo' &安培; '移动杆,品牌,标志&#39 ;.我可以改变其中任何一个,但不能同时改变它们。

我似乎无法弄明白我需要把它放在哪里。有人可以帮忙吗?

PUT {VX}/accounts/{accountid}/users/

{
    "users": [{
        "userId": "00000000-0000-0000-0000-000000000000",
        "sendActivationEmail": "send"
    }]
}

更新

感谢下面的天才,这对我来说很有用

<script>
   window.onload = function() {
     document.getElementsByClassName('Header-branding-logo')[0].src = 'https://static1.squarespace.com/static/5a9e8da0aa49a18379207907/t/5ac52f062b6a28a603df30cf/1522872070340/GBL+Shop+-+Black+1500.png';  
   };
 </script>

2 个答案:

答案 0 :(得分:0)

请使用querySelectorAll功能

var elements = document.querySelectorAll('.Header-branding-logo,.Mobile-bar-branding-logo');

elements.forEach((element) => {
    element.src = "https://static1.squarespace.com/static/5a9e8da0aa49a18379207907/t/5ac52f062b6a28a603df30cf/1522872070340/GBL+Shop+-+Black+1500.png"
})

答案 1 :(得分:0)

   <script>
window.onload = function() {
    var classes = document.querySelectorAll(".Header-branding-logo, .Mobile-bar-branding-logo") // Gets multiple classes whereas getElementsByClassName() fetches only one. Also dont miss the '.' before classnames here
    for(i = 0; i <= classes.length; i++){
        classes[i].src = "https://static1.squarespace.com/static/5a9e8da0aa49a18379207907/t/5ac52f062b6a28a603df30cf/1522872070340/GBL+Shop+-+Black+1500.png"
    }
}