So I have this code that should listen for a click on #button but it won't work, and is driving me crazy!
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script ty开发者_Python百科pe="text/javascript">
$('#button').click(function() {
alert('OK!');
});
</script>
and the HTML:
<input id="button" type="button" value="OK" />
This is strange. Any help is welcome!
Write your code inside document.ready function
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script type="text/javascript">
$(document).ready(function() {
$('#button').click(function() {
alert('OK!');
});
});
</script>
OR
<script type="text/javascript">
function on_click() {
alert("OK !!");
}
$(document).ready(function() {
$("#button").click(on_click);
});
</script>
hope you get some idea from this
Here's what the code should look like:
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script type="text/javascript">
// Now that jquery is include, place the code to wire up the button here
$(function(){
// Once the document.onload event fires, attach the click
// event handler for the button
$('#button').click(function() {
alert('OK!');
});
});
</script>
<input id="button" type="button" value="OK" />
Here's the jquery documentation that relates to this: http://docs.jquery.com/How_jQuery_Works
精彩评论