簡體   English   中英

如何通過單擊頁面運行 JavaScript 函數?

[英]How can I run a JavaScript function with any click on the page?

現在我正在使用<body onload="function"> ,它根據關注的元素更改頁面上的一些文本。 它工作正常,但我需要我的網站在每次焦點改變時運行該功能(或者更好的是,每次點擊頁面的任何部分時)。

目前代碼如下所示:

<body onload="changeText()">
<script>
function changeText(){
     function here;
}
</script>
<p>This is where text changes based on the function</p>

謝謝!

您可以在文檔上附加 onclick 事件。 提琴手

​document.onclick = function(){
 // your code
 //alert("clicked");
}​

如果你想改變焦點然后使用

window.onfocus = function(){

   // your code
     //alert("focus");
}

 window.onclick = function () { //do some stuff document.body.style.backgroundColor = "red"; }
 <p>text text text</p> <button>it does not matters if you press me or any where else</button>

試試這個:

<!-- language: lang-html -->
<body onload="changeText()">
<script>
function changeText(){
    function here;
}
document.onclick=function(){changeText();};
</script>
<p>This is where text changes based on the function</p>

jQuery 不會受到禁用點擊問題的影響

$(document).click(function(e){
                //Blah
            })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM