Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the body element using jquery

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

like image 590
saurabh ranu Avatar asked Nov 07 '11 11:11

saurabh ranu


People also ask

Which methods return the element as a jQuery object?

The jQuery selector finds particular DOM element(s) and wraps them with jQuery object. For example, document. getElementById() in the JavaScript will return DOM object whereas $('#id') will return jQuery object.

Is it possible to access the underlying DOM element using jQuery?

The Document Object Model (DOM) elements are something like a DIV, HTML, BODY element on the HTML page. A jQuery Selector is used to select one or more HTML elements using jQuery. Mostly we use Selectors for accessing the DOM elements.

What does $(' body ') mean in jQuery?

$(document. body) is using the global reference document to get a reference to the body , whereas $('body') is a selector in which jQuery will get the reference to the <body> element on the document .

How do I get HTML using jQuery?

To get HTML content of an element using jQuery, use the html() method. The html() method gets the html contents of the first matched element.


3 Answers

Try this...

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

the ,html allows 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.

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

an element can be selected by id using the prefix # so

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

$('#c_1');

or by class with the prefix .

<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.

body, html {overflow: hidden;}

the overflow parameter allows you to control what happens when content overflow the assigned width and hight.

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

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

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

like image 73
Val Avatar answered Sep 28 '22 00:09

Val


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

 $("body").css("overflow", "hidden");
like image 44
Pedryk Avatar answered Sep 28 '22 00:09

Pedryk


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

It's simply:

document.body;

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

Example Here

document.body.style.overflow = 'hidden';
like image 45
Josh Crozier Avatar answered Sep 28 '22 01:09

Josh Crozier