


This series of articles mainly talks about the basic knowledge of implementing Javascript drag and drop function, and will give a complete example at the end. Suitable for people who don’t know anything about drag and drop.
Let’s first talk about the offsetParent attribute in Javascript.
Supported browsers: Internet Explorer 4.0, Mozilla 1.0, Netscape 6.0, Opera 7.0, Safari 1.0
element.offsetParent
Summary
offsetParent returns a reference to the object which is the closest (nearest in the containment hierarchy) positioned containing element. If the element is non-positioned, the root element (html in standards compliant mode; body in quirks rendering mode) is the offsetParent. offsetParent returns null when the element has style.display set to "none ".
Syntax
parentObj = element.offsetParent
Parameters
· parentObj is an object reference to the element in which the current element is offset.
Specification
DOM Level 0. Not part of specification.
Excerpt from Mozilla Developer Center website
Translated as follows:
element.offsetParent
Summary
The offsetParent property returns a reference to an object that is closest to the element calling offsetParent (the closest in the containing hierarchy) and is the container element that has been CSS positioned. If this container element is not CSS positioned, the value of the offsetParent attribute is a reference to the root element (html element in standards compatibility mode; body element in weird rendering mode). When the container element's style.display is set to "none" (Except for IE and Opera), the offsetParent property returns null .
Syntax
parentObj = element.offsetParent
Variables
· parentObj is a reference to an element in which the offset of the current element is calculated.
Specification
DOM Level 0. Not part of the specification.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <script type="text/javascript" language="JavaScript"> function offset_init(){ var pElement = document.getElementById("sonObj"); parentObj = pElement.offsetParent; alert(parentObj.tagName); } </script> </head> <body onload="offset_init()"> <div id="parent"> <p id="sonObj">测试OffsetParent属性</p> </div> </body> </html>
Test results:
Firefox3: "BODY"
Internet Explorer 7: "BODY"
Opera 9.51: "BODY"
Chrome 0.2: "BODY"
Safari 3: "BODY"
Conclusion:
When neither an element nor its parent element is CSS positioned, then The value of the offsetParent attribute of this element is the root element. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is the Body element. (In fact, regardless of the standard compatibility mode or the weird mode, the root element is the Body element)
Test code 2:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> #parent{ position:absolute; <!--position:relative;--> left:25px; top:188px; border:1px solid black; } </style> <script type="text/javascript" language="JavaScript"> function offset_init(){ var pElement = document.getElementById("sonObj"); parentObj = pElement.offsetParent; alert(parentObj.tagName); } </script> </head> <body onload="offset_init()"> <div id="parent">div测试代码<p id="sonObj">测试OffsetParent属性</p></div> </body> </html>
Test result:
Firefox3: "p"
Internet Explorer 7: "p"
Opera 9.51: "p"
Chrome 0.2: "p"
Safari 3: "p"
Conclusion:
When the parent element of an element has CSS When positioned (absolute or relative), the value of the offsetParent attribute of this element is its parent element. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is its parent element.
Test code 3:
[Ctrl A to select all Note: If you need to introduce external Js, you need to refresh to execute]
Test results:
Firefox3: "H1"
Internet Explorer 7: "H1"
Opera 9.51: "H1"
Chrome 0.2: "H1"
Safari 3: "H1"
Conclusion:
When someone When neither element nor its parent element is CSS positioned (absolute or relative), the value of the offsetParent attribute of this element is the element closest to it in the DOM structure hierarchy and which has been CSS positioned.
Test code 4:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> <style type="text/css"> #Grandfather{ position:relative; left:25px; top:188px; border:1px solid black; } </style> <script type="text/javascript" language="JavaScript"> function offset_init(){ var pElement = document.getElementById("sonObj"); parentObj = pElement.offsetParent; alert(parentObj.tagName); } </script> </head> <body onload="offset_init()"> <h1 id="Grandfather"> <div id="parent"> <p id="sonObj">测试OffsetParent属性</p> </div> </h1> </body> </html>
Test result:
Firefox3: "BODY"
Internet Explorer 7: "BODY"
Opera 9.51: " BODY"
Chrome 0.2: "BODY"
Safari 3: "BODY"
Conclusion:
When only a certain element is CSS positioned, neither its parent element nor its DOM structure level is positioned. When CSS is positioned, the value of the offsetParent attribute of this element is HTMLBODYElement. To be more precise, the reference for various offset calculations (offsetTop, offsetLeft, etc.) of this element is the Body element.
Okay, that’s it for the offsetParent attribute.
As for what to write next, I haven’t decided yet.

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
