0

我有两个问题:

首先,我如何更改此功能,以便在用户单击 id 为“canvascontent”的 div 时触发它:

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

更重要的是,我如何定义下面按钮的功能只有在上面的功能首先被触发时才被触发?

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

谢谢!

4

2 回答 2

1

Event.Add不是一件事,但Event 对象是.

//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">
于 2013-07-31T18:49:55.613 回答
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>
于 2013-07-31T18:55:36.900 回答