Get all elements with `position:fixed` in an HTML page?

前端 未结 4 1079
梦毁少年i
梦毁少年i 2021-01-05 03:07

Reason for doing that: I\'m debugging css of my webpage.. some elements appeared and they\'re not supposed to appear. I suspect it is the issue with element positioning.. th

相关标签:
4条回答
  • 2021-01-05 03:23

    Try this:

    var elements = $('*').filter(function () { 
        return this.style.position == 'fixed';
    });
    

    It will give you all elements having position fixed.

    0 讨论(0)
  • 2021-01-05 03:25

    This one is using jQuery. I hope you are find with it.

     var find = $('*').filter(function () { 
            return $(this).css('position') == 'fixed';
        });
    

    I think this one works using a pure javascript:

    var elems = document.body.getElementsByTagName("*");
    var len = elems.length
    
    for (var i=0;i<len;i++) {
    
        if (window.getComputedStyle(elems[i],null).getPropertyValue('position') == 'fixed') {
            console.log(elems[i])
        }
    
    }
    
    0 讨论(0)
  • 2021-01-05 03:39

    document.querySelector('*[style="position:fixed"]')

    The * item specifies all tag names. The [] indicate that you're looking for an attribute. You want your style attribute to have position:fixed.

    If you aren't using jQuery, this is probably going to be your simplest option.

    0 讨论(0)
  • 2021-01-05 03:40

    Here is an ES6 version that gives you an array of these elements for further processing:

    let fixedElements = [...document.body.getElementsByTagName("*")].filter(
        x => getComputedStyle(x, null).getPropertyValue("position") === "fixed"
    );
    
    0 讨论(0)
提交回复
热议问题