Javascript only affects one element

后端 未结 2 1770
执念已碎
执念已碎 2021-01-23 12:18

I don\'t understand, the border is only visible on the first item, why?

HTML

2条回答
  •  执念已碎
    2021-01-23 12:59

    You can use document.getElementsByClassName

    JavaScript

    var elements = document.getElementsByClassName('item');
    for (i =0; i < ele.length; i++)    {
        elements[i].style.borderTop = '1px solid #000';
    }
    

    HTML

    Content 1
    Content 2

    CSS

    #theContainer {
        height: 100px;
        width: 500px;
        position: relative;
        border: 1px solid #900;
        padding: 10px;
    }
    .item {
        height: 50px;
        width: 100%;
    }
    ​
    

提交回复
热议问题