简体   繁体   中英

OnClick Javascript doesn't seem to be working properly

I'm doing a project that involves a console-like panel. I've looked around on Google, but I can't solve this problem! I got the load() working, but the click() isn't.

<html>

<head>
<script type="text/JavaScript">
function load()
{
  document.getElementById("display").innerHTML="Hello!";
}

function click()
{
  alert("Clicked");
}
</script>
</head>

<body style="background-color:red;" onload="load()">
<div id="display" style="position:absolute; right:0px; left:0px; bottom:0px; width:100%; height:87%; background-color:white;"></div>
<div style="position:absolute; left:0px; width:10%;">
<b>Input:</b>
<input id="text" maxlength=50 />
<input type="button" onclick="click()" value="OK" />
</div>

</body>
</html>

Help if you can, please! Thanks. :)

Just rename your click function to something else, eg myclick and it will work :)

It happens because calling click() you trigger the click event again but not your function.

Check it here : http://jsfiddle.net/rHyZ7/

I experienced the same issue when naming my function clear()

changing the function name fixed the issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM