Javascript 如何使用jquery获取body元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8035695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:28:44  来源:igfitidea点击:

How to get the body element using jquery

javascriptjqueryhtml

提问by saurabh ranu

I want to get the body element of an html using jquery and then disable the vertical scrolling?

我想使用 jquery 获取 html 的 body 元素,然后禁用垂直滚动?

回答by Val

Try this...

尝试这个...

$('body,html').css('overflow','hidden');

$('body,html').css('overflow','hidden');

the ,htmlallows you to also include the <html>because some browsers may use that as the base as oppose to the <body>tag so it helps treating both as if they were one.

,html让你还包括<html>由于一些浏览器可以使用它作为基地作为反对的<body>标记,以便它可以帮助标本兼治,好像他们是一个。

helpfulany tag should be selected like so $('a');or $('body');

有用的任何标记应选择像这样$('a');$('body');

an element can be selected by idusing the prefix #so

可以id使用前缀来选择元素,#所以

<a id="c_1" >CLick Me</a>

<a id="c_1" >CLick Me</a>

$('#c_1');

or by classwith the prefix .

或通过class前缀.

<a class="classname">Click Me</a>

<a class="classname">Click Me</a>

$('.classname');

for more information read http://api.jquery.com/category/selectors/as for the scroll bars they are controlled by css, you could simply go on a css file and do the following.

有关更多信息,请阅读http://api.jquery.com/category/selectors/至于它们由 css 控制的滚动条,您可以简单地访问 css 文件并执行以下操作。

body, html {overflow: hidden;}

body, html {overflow: hidden;}

the overflowparameter allows you to control what happens when content overflowthe assigned width and hight.

overflow参数允许您控制当满足overflow指定的宽度和高度时会发生什么。

http://reference.sitepoint.com/css/overflow

http://reference.sitepoint.com/css/overflow

or the proper reference http://www.w3.org/TR/CSS2/visufx.html

或适当的参考http://www.w3.org/TR/CSS2/visufx.html

sorry if this is too technical, but one day you would have to learn about these :)

抱歉,如果这太技术化了,但总有一天你将不得不了解这些:)

回答by Pedryk

You can get the body element with: $("body")then disable the scrollbars with CSS

您可以使用以下方法获取 body 元素:$("body")然后使用 CSS 禁用滚动条

 $("body").css("overflow", "hidden");

回答by Josh Crozier

Since nobody mentioned this, it's pretty easy to select the bodyelement without jQuery.

由于没有人提到这一点,所以body不用 jQuery就可以很容易地选择元素。

It's simply:

很简单:

document.body;

And based on your question, you would disable vertical scrolling by setting overflow: hidden:

根据您的问题,您可以通过设置禁用垂直滚动overflow: hidden

Example Here

示例在这里

document.body.style.overflow = 'hidden';