I have a long rectangular div (parent) with a few smaller divs (children) inside it. I want to click anywhere on a long rectangular div (including child divs) to show the offset to the left of the parent div.
Currently, this javascript code only works when I click outside the small divs, as soon as I click inside the child div, it shows me the offset of the child div. I am completely new to javascript and cannot find any suitable answers to this question.
I hope someone can help, thanks in advance.
window.onload = function(){ parentDiv.addEventListener('click',function(e){ alert(e.offsetX); }, false);
getClientRects clientX, :
getClientRects
clientX
var offset = this.getClientRects()[0]; alert(e.clientX - offset.left);
DEMO
-, DOM. DOM, Javascript, . , .
, div, , , div. , , , div. , , arg e. . offsetX, , , IE ( Opera), Chrome, Safari Firefox. doc Microsoft, offsetX()" x . , div, , , .
div
e
offsetX
offsetX()
JavaScript, : - JavaScript, , API. , , IE. jQuery - ( ). jQuery normalized .
Source: https://habr.com/ru/post/1532398/More articles:Error OpenBUGS undefined - rRead server response using OkHttp - androidUnit Test Coverage is pedantic - c #PHP namespacing and spl_autoload_register - phpтег изображения не включает дайджест ресурсов по электронной почте - ruby-on-rails-4iOS 7: UINavigationBar not in UINavigationController does not make transparency transparent - iosSound back with equal probability every time - javaAngular memory leak - angularjsShould I only deploy .pyc files on the server if I am worried about code security? - pythonExplicit conversion from int -> sbyte -> sbyte? - c #All Articles