开发者

JavaScript: Whats the difference between 'Document' and 'HTML'

Example:

$(document).click(function() {开发者_Python百科 blah });   
// and
$('html').click(function() { blah });


I'll answer the question in several parts.

In JavaScript (not just jQuery, but all JavaScript) the document keyword is a handle on the object that contains the HTMLDocument. You might use this handle in the following scenarios...

// Get the current web address
alert(document.location.href);

When you pass the document to jQuery, it parses the document into a jQuery object.

When you pass the "html" selector to jQuery, it uses this string to find any elements in the document object model that match the selector (in all cases, there will be one html element).

In reality, you won't notice a difference in behaviour between these:

$(document).click(function() { alert('blah'); });   
$('html').click(function() { alert('blah'); });
$('body').click(function() { alert('blah'); });

But the technical difference is that document is an object and 'html' is a string that is used to search for an element. Both the object and any matching elements are converted into jQuery objects.

As they all add a click event handler to the "visible" part of the page, which is the only part of the page a user can realistically click on.


try to put out the innerHTML of both, whats the result? i reckon (but han't tested) that

  • document is really the complete document, including <html> and all elements in it
  • html references the <html>-tag, so there will only be the <head> and <body> in your output, not the <html>-tag itself

but: for your code (adding an click-handler) there would be no difference, because clicking into the document will always be a click on the <html> (as long as you site is valid and has an <html>-tag)


They select the same thing. The only difference is how jQuery's sizzle engine finds it. The first case being a special case in the jQuery init function, the second one using the getElementsByTagName


Few differences:

  1. Both document and <html> are Nodes
  2. document is an instance of Document whereas <html> is an instance of Element
  3. document includes the <!DOCTYPE...> tag as well as the <html> tag

// document
console.log('-----------document inheritance chain------------')
console.log(document.__proto__.constructor.name)
console.log(document.__proto__.__proto__.constructor.name)
console.log(document.__proto__.__proto__.__proto__.constructor.name)
console.log(document.__proto__.__proto__.__proto__.__proto__.constructor.name)
console.log(document.__proto__.__proto__.__proto__.__proto__.__proto__.constructor.name)


// html element
const HTML = document.getElementsByTagName('html')[0]
console.log('-----------<html> inheritance chain------------')
console.log(HTML.__proto__.constructor.name)
console.log(HTML.__proto__.__proto__.constructor.name)
console.log(HTML.__proto__.__proto__.__proto__.constructor.name)
console.log(HTML.__proto__.__proto__.__proto__.__proto__.constructor.name)
console.log(HTML.__proto__.__proto__.__proto__.__proto__.__proto__.constructor.name)
console.log(HTML.__proto__.__proto__.__proto__.__proto__.__proto__.__proto__.constructor.name)


console.log('-----------document child-nodes------------')
console.log(document.childNodes.length);
console.log(document.childNodes[0].name);
console.log(document.childNodes[1] === HTML);

Good Luck...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜