最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How can I run a JavaScript function with any click on the page? - Stack Overflow

programmeradmin0浏览0评论

Right now I'm using <body onload="function">, which changes some text on the page based on which element is focused on. It works fine, but I need my site to run the function every time the focus changes (or, even better, every time any part of the page is clicked).

Currently the code looks like this:

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

Thanks!

Right now I'm using <body onload="function">, which changes some text on the page based on which element is focused on. It works fine, but I need my site to run the function every time the focus changes (or, even better, every time any part of the page is clicked).

Currently the code looks like this:

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

Thanks!

Share Improve this question edited Oct 11, 2012 at 17:57 freefaller 20k7 gold badges61 silver badges95 bronze badges asked Oct 11, 2012 at 17:56 swaysway 3732 gold badges5 silver badges17 bronze badges 5
  • I think he's asking what to hook the onclick to. – Robert Harvey Commented Oct 11, 2012 at 17:58
  • 2 Why are people down voting this legitimate question? – Anthony Hatzopoulos Commented Oct 11, 2012 at 18:02
  • @RASG so what, same goes for an extremely high percentage of most of the questions on SO, including the ones with high votes. Searching SO I couldn't find a similar question. And that is what matters. – Anthony Hatzopoulos Commented Oct 12, 2012 at 16:10
  • 1 @AnthonyHatzopoulos Searching SO I couldn't find a similar question. And that is what matters. oh really? stackoverflow.com/questions/3521487/javascript-body-onclick , stackoverflow.com/questions/9152802/… , stackoverflow.com/questions/12762472/… – yodog Commented Oct 12, 2012 at 16:21
  • @RASG awesome & +1 for you! btw I found that first one too, you must of really searched hard for those or not; you just know of the onclick event where as sway doesn't. The 2nd SO link is the best but if you have no idea what onclick is to begin with this question's title is better for the beginner. I wouldn't consider the 1st link related, especially to a beginner. The 3rd is close but still for a beginner is not obvious especially with textarea in the title. – Anthony Hatzopoulos Commented Oct 12, 2012 at 16:53
Add a comment  | 

4 Answers 4

Reset to default 8

You can attach onclick event on document. jsfiddle

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

If you want change on focus then use

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>

Try this:

<!-- 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 doesn't suffer from the disable-clicking issue

$(document).click(function(e){
                //Blah
            })
发布评论

评论列表(0)

  1. 暂无评论