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.