Use JavaScript to obtain the code of the object where the cursor is placed on the page and the event is triggered

Source: Internet
Author: User

Use JavaScript to get the mouse position: CopyCode The Code is as follows: function mouseposition (EV ){
If (EV. pagex | eV. Pagey ){
Return {
X: eV. pagex,

Y: eV. Pagey
};
}
Return {
X: eV. clientx + document. Body. scrollleft-document. Body. clientleft,

Y: eV. clienty + document. Body. scrolltop-document. Body. clienttop
};
}

Document. onmousemove = mousemove;
Function mousemove (EV ){
Ev = EV | window. event;
VaR mousepos = mouseposition (EV );
}

A detailed description of the Code, which has been introduced in the original article, is now transferred here:
we must first declare an evnet object, whether it is moving, clicking, or pressing a key, will activate an evnet. in Internet Explorer, event is a global variable and will be stored in window. event. in Firefox or other browsers, events are obtained by corresponding functions. when we assign the mousemove function to document. onmousemove, mousemove will get the mouse movement event.
to allow eV to obtain Event Events in all browsers, "| window. Event" in Firefox does not work because EV has a value assignment. EV is empty in MSIE, so the window. event is obtained.
in this Article , we need to obtain the mouse position multiple times. Therefore, we designed a mouseposition function, it contains an event parameter.
because we want to run in MSIE and other browsers, Firefox and other browsers use event. pagex and event. pagey indicates the position of the mouse relative to the document. If you have a 500*500 window and your mouse is in the absolute middle, the pagex and Pagey values are both 250, if you scroll down 500, Pagey will become 750.
the opposite is true for MSIE. It uses event. clientx and event. clienty to indicate the location of the window, rather than the document. In the same example, If you scroll down to 500, clienty is still 250. Therefore, we need to add the attributes of scrollleft and scrolltop relative to the document. Finally, the document in MSIE does not start from 0 to 0, but usually has a small border (usually 2 pixels). The border size is defined in document. body. in clientleft and clienttop, we also add these.
fortunately, we have used the mouseposition function to solve the coordinate problem, so we don't have to worry about it.

Use JavaScript to obtain the object that triggers the eventCopy codeThe Code is as follows: <script language = "JavaScript">
Document. onclick = onclick;
Function onclick (EV)
{
Ev = EV | window. event; // event
VaR target = ev.tar GET | eV. srcelement; // obtain the event Source
/* Target. getattribute () is used to obtain some attributes of the event source object.
For example, the object contains (name, ID, type, etc );*/
VaR dragobj = target. getattribute ('id ');
Alert (dragobj );
}
</SCRIPT>

The key is the compatibility of event objects in multiple browsers, which is the same as the above method. The code here will not be explained.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.