How to find the number of clicks?

I want to print the number of clicks for the console, but this should be for all empty areas. Is it possible?

I tried with the button, but could not do what I want.

HTML:

   <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <script src="myScript3.js"></script>
        </head>

        <body>
            <button onclick="tiklama()">Tıkla!</button>
        </body>
    </html>

JS:

var x = 0;
function tiklama() {
    console.log(++x);
}
+4
source share
4 answers

Listen to the click event at a higher level, for example. on the element <html>and check if the event property matches targetyour definition of the "blank area".

var counter = 0;
document.documentElement.addEventListener('click',
    function(e) {
        // counting all clicks except on the interactive elements
        if (!e.target.matches('button,input,textarea,select')) {
            console.log(++counter);
        }
    },
    false);
<p>Some text -- considered non-interactive area.</p>
<button>An interactive button</button>
Run codeHide result
+2
source

var x = 0;
function tiklama() {
    alert(++x);
}
<button onclick="tiklama()">Tıkla!</button>
Run codeHide result
0
source

, , (html), :

var x = 0;
$('html').click(function() {
    ++x;
  document.getElementById("number").innerHTML = x;
});

jsFiddle

0

, , , , :

var body = document.getElementsByTagName("body")[0];
document.addEventListener("click", e => tickText.textContent += "tick..! ");
myButton.addEventListener("click", e => e.stopPropagation());
<button id="myButton">Tıkla!</button>
<p id="tickText"></p>
Hide result
0

Source: https://habr.com/ru/post/1651531/


All Articles