How to change the “html” element's CSS

瘦欲@ 提交于 2019-12-02 09:39:05
Samuel

There's probably a simpler way, but

htmlTags = document.getElementsByTagName("html")
for(var i=0; i < htmlTags.length; i++) {
    htmlTags[i].style.overflowY = "hidden";
}

Hope I remembered everything right.

Seems like you would want to learn how to use a javascript framework/toolkit:

First of all, I would not recommend setting styles on the html element directly. The body tag is meant to be the top node of the DOM display list, and Firefox will interpret styles applied to html as styles applied to body anyway. Other browsers may behave differently.

As beggs mentioned, I would recommend learning one of the popular javascript frameworks. They make things like this (HTML traversing and manipulation) a little easier. As it stands, you can write the following code using standard DOM methods. This requires an element with an id of "button" placed somehwhere in your markup.

<a id="button" href="#">Action!</a>

Add the following to a script tag in <head>, or in an external script (recommended).

window.onload = function(e) {

    var button = document.getElementById("button");

    button.onclick = function(e) {
        document.body.style.overflowY = "hidden";
        return false;
    }
}

Alternatively, if you want to use jQuery:

$(document).ready(function() {
    $("#button").click(function(){ 
        $(document.body).css({ overflowY: "hidden" );
    });
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!