Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在特定容器中单击时运行函数_Javascript - Fatal编程技术网

Javascript 在特定容器中单击时运行函数

Javascript 在特定容器中单击时运行函数,javascript,Javascript,我有两个问题: 首先,如何更改此函数,以便在用户单击id为“canvascontent”的div时触发此函数: 更重要的是,当上面的功能先被触发时,我如何定义下面按钮的功能才被触发 <input type="button" onclick="sketch.toDataURL()" style="" value="Speichern"> 谢谢 事件。添加不是一件事,而是一个问题 然后 <input type="button" onclick="sketchData()" s

我有两个问题:

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

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

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


谢谢

事件。添加
不是一件事,而是一个问题

然后

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

下面是一个完整的示例,说明您想要做什么:

<!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>

var buttonClicked=false;
函数加载()
{
//动态添加事件处理程序。也可以用html编写,如下所示:
// 
var div=document.getElementById(“canvascontent”);
div.setAttribute('onclick','divClick()')
}
函数按钮单击()
{
如果(buttonClicked==true)
{
警惕(“完成”);
sketch.toDataURL();
}
}
函数divClick()
{
buttonClicked=true;
}
div的内容

什么是
事件。添加
?您是否尝试过将
窗口
替换为
document.getElementById('canvascontent')
div.setAttribute('onclick','divClick()')
实际上应该是
div.addEventListener('click',divClick)
<input type="button" onclick="sketchData()" style="" value="Speichern">
<!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>