2013-07-31 41 views
0

我有兩個問題:上點擊以特定於容器中運行的功能

首先,我怎樣才能改變這種功能,因此,這將是triggerd當id爲「canvascontent」股利用戶點擊:

Event.add(window, "load", function() { 

更重要的是,我怎樣才能定義下面的按鈕的功能只有當上面的功能先被觸發時才觸發?

<input type="button" onclick="sketch.toDataURL()" style="" value="Speichern"> 

謝謝!

+0

什麼'Event.add'?你有嘗試用'document.getElementById('canvascontent')'替換'window'嗎? –

回答

1

Event.Add不是東西,而是Event object is

//Set a variable so both functions have scope to it. 
var canvasClicked = false; 

//Add the 'click' event to the DOM element "canvascontent" 
//You could also use .addEventListener() 
document.getElementById("canvascontent").onclick = function(e) { 
    //do whatever 
    canvasClicked = true; 
}; 

//Call sketch data when the button is clicked 
function sketchData() { 
    //only sketch to the DataURL if the canvas has been clicked. 
    if (canvasClicked) { 
     sketch.toDataURL() 
    } 
} 

然後......

<input type="button" onclick="sketchData()" style="" value="Speichern"> 
0

這裏有你想要做什麼一個完整的例子:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
    <script type="text/javascript"> 

     var buttonClicked = false; 
     function load() 
     { 
      //Adds the event handler dynamicly. Could also be written in html like so: 
      // <div id="canvascontent" onclick="divClick()"> 
      var div = document.getElementById("canvascontent"); 
      div.setAttribute('onclick', 'divClick()') 
     } 

     function buttonClick() 
     { 
      if (buttonClicked == true) 
      { 
       alert("DONE"); 
       sketch.toDataURL(); 
      } 
     } 

     function divClick() 
     { 
      buttonClicked = true; 
     } 
    </script> 
</head> 
<body onload="load()"> 
    <div id="canvascontent"> 
     Content of div 
    </div> 
    <input type="button" onclick="buttonClick()" style="" value="Speichern"> 
</body> 
</html> 
+0

'div.setAttribute('onclick','divClick()')'應該是'div.addEventListener('click',divClick)''。 –