How to target div at the end of the row?

前端 未结 3 389
萌比男神i
萌比男神i 2021-01-06 10:18

I am trying to insert a box strip in between rows of divs, how can I target the end of each row\'s div?

Here\'s a JSFiddle of the divs: http://jsfiddle.net/5Sn94

相关标签:
3条回答
  • 2021-01-06 10:47

    Using jQuery, you can use the last-child selector: http://jsfiddle.net/5Sn94/1/

    0 讨论(0)
  • 2021-01-06 11:08

    You can use document.getElementsByTagName but it will only work if you don't have any other divs on the page http://jsfiddle.net/5Sn94/2/

    var divs = document.getElementsByTagName('div');
    divs[divs.length-1].style.border = '1px solid red';
    
    0 讨论(0)
  • 2021-01-06 11:14

    I think this can help you in your investigations:

    div {
        display: inline-block; // <-- use display: inline-block instead of float: left
        margin: 0 15px 0 0;
        cursor: pointer;
    }
    .expander {
        background: coral;
        height: 100px;
        width: 100%;
        margin-bottom: 6px;
        float: left; // <-- this float makes the trick
    }
    

    http://jsfiddle.net/5Sn94/14/

    .expander is a div which needs to be inserted after target (hovered in my example) element. It has 100% width to occupy whole horizontal space.

    To insert expander after hovered div I used this javascript (inside for loop):

    div[i].onmouseover = function() {
        d.innerHTML = 'Details about div #' + this.dataset.id;
        this.parentNode.insertBefore(d, this.nextSibling).style.display = 'block';
    }
    

    You can insertAfter in jQuery, etc.

    0 讨论(0)
提交回复
热议问题