简体   繁体   中英

What does $(document.body) mean in javascript?

这是什么意思?

The document.body in javascript is a direct reference to the DOM element representing the <body> portion of the page.

The $() part depends on how it is used. $ could be a variable name, and () after a variable or property name attempts to call a function stored in that variable or property.

So if you have:

var $ = function() { alert('howdy'); };

Then this:

$();

...will call that function, and trigger the alert.

Functions can accept arguments, so you could modify the function above to accept the document.body element as an argument, and alert() its innerHTML (for example);

  // alerts the innerHTML of the element it receives
var $ = function( elem ) { alert( elem.innerHTML ); }; 

$( document.body ); // Passes the element when calling the $ function

$ is the name of a function. It is being passed the document's body DOM element. Typically $ is used to represent a JavaScript library. Most commonly jQuery . In jQuery it selects the body element.

查看“$(document.body)”的SymbolHound结果

它将DOMElement引用传递给jQuery对象/函数,以便返回一个jQuery对象,其中[0]包含引用,而上下文是正文。

I think this has meaning in jquery or prototype (or other frameworks), not in pure javascript. $ is a function, in prototype it extends document.body with framework methods.

You're simply passing an argument to a function named "$"

function $(someargument){
    ....
}

In this case the argument being passed is document.body

Typically Jquery uses $, so in that case probably someone wanted to use Jquery functions directly on the body ie wrapping the body in jquery.

$(document.body).html("hi");

(probably not a good idea to do that, but you get the idea)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM