I wrote this code:
<div onclick="f()"></div>
But this code doesn't work on some systems, I think "onclick" may work with other tags properly, Is there anybody have same experience with this problem?
I wrote this code:
<div onclick="f()"></div>
But this code doesn't work on some systems, I think "onclick" may work with other tags properly, Is there anybody have same experience with this problem?
Your onclick works fine.
If you have it on a div, you might have problems of it not actually having any size (often an empty div will have 100% width and 0px height, unless something's in it).
I'd suggest double checking that your div has size to it.
Here's a working jsbin:
I don't like use javascript mixed with HTML, when you have a large app debug would be messy, I prefer delegate an onClick event on this fashion:
<button id="my_button">Click me!</div>
<script>
(function () {
var btn = document.getElementById('my_button');
btn.addEventListener('click', function (event) {
alert('Click!');
});
}());
</script>
In the code before we are setting an event listener to the button which will invoke that callback function. I have used a button, but it is the same with a div. The live example: http://jsfiddle.net/TonyMtz/pT4nw/2/
Cheers.