Javascript-implemented element drag function host is browser, javascript host

Source: Internet
Author: User

Javascript-implemented element drag function host is browser, javascript host

// Host is the browser
// Pass a reference to the corresponding element object into the function
function candrag (drager) {
  drager.onmousedown = function (down) {
    var offx = drager.offsetLeft
    var offy = drager.offsetTop;
    var offxl = down.clientX-offx;
    var offyl = down.clientY-offy;
    window.condition = 0; // Added the condition attribute to window, used to resolve the contradiction between click
    document.onmousemove = function (move) {
      drager.style.left = move.clientX-offxl + "px";
      drager.style.top = move.clientY-offyl + "px";
      drager.style.cursor = "move";
      condition = Math.abs (move.clientX-down.clientX) + Math.abs (move.clientY-down.clientY);
    }
  }
  drager.onmouseup = function () {
    document.onmousemove = null;
    draggerr.style.cursor = "auto";
  }
}
/ * For conflict resolution with click, you need to determine the condition
*E.g: 
candrag (dragger);
d01.onclick = function () {
  if (! condition) {
    d01.style.backgroundColor = "red";
  }
}
* Where d01 is a child of dragger
* / 

Why is my javascript function unable to redirect a browser to a page?

How do you call it? Both methods are required. Try...
Method 1: This is called through a webpage.
<Html>
<Head>
<Meta http-equiv = "Content-Language" content = "zh-cn">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Create web page 1 </title>
</Head>
<SCRIPT language = "javascript">
<! --

Function ChangeQty (pn)
{

Window. location. href = "www.sohu.com ";

Window. open ('www .sohu.com ');

}

// --> </SCRIPT>
<Body onload = "ChangeQty ()">
</Body>

</Html>
------------------------------

Method 2: This is called by clicking.
<Html>

<Head>
<Meta http-equiv = "Content-Language" content = "zh-cn">
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Create web page 1 </title>
</Head>

<Body>
<SCRIPT language = "javascript">
<! --

Function ChangeQty (pn)
{

Window. location. href = "www.sohu.com ";

Window. open ('www .sohu.com ');

}

// --> </SCRIPT>
<A href = "#" o ...... remaining full text>

How can I use javascript to add a response function to an HTML button element after obtaining its id using javascript?

The binding event response does not have to use an id. Check whether you have obtained this element before your description?
Assume that the element to be bound is btn, and the event to be bound is clicked with the mouse, then:
Btn. onclick = function (){...}; // directly associate the event to respond to btn. addEventListener ('click', function (e ){...}, false); // The advanced browser supports btn. attachEvent ('onclick', function (){...}); // earlier versions of IE support binding the click Event Response on btn in the above three methods. Difference: first, bind the event directly. If btn has already been bound to a response function, the last two methods are Event Response registration methods. Multiple Response functions can be registered, which do not affect each other.
If some frameworks (such as jQuery) are used, you can use the compatibility method encapsulated by the Framework to register the event response.

Related Article

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.