开发者

Show javascript alert and do other actions?

开发者 https://www.devze.com 2023-03-01 16:52 出处:网络
Here\'s my code and nothing is happening: <开发者_如何学JAVA!DOCTYPE html> <html> <head>

Here's my code and nothing is happening:

<开发者_如何学JAVA!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-git.js"></script>

<script>
$(".clickMe").click(function() {
   alert('hi!');
   // Do other stuff
});
</script>

</head>
<body>

<div class="clickMe">Click Me!</div>

</body>
</html>

However, nothing happens when you click the "clickMe" div.

Thanks!


Let the document be ready

$(function(){
    $(".clickMe").click(function() {
       alert('hi!');
       // Do other stuff
    });
})

The way you had it, the div is not yet available (the DOM is not loaded) so, no click handler is added. You need to wait for document to be available. The .ready() is to be used for that.

$(document).ready(function() {...})
$(function() {...}) - Shortcut


Try wrapping the jQuery code inside this:

$(document).ready(function() {
  // Handler for .ready() called.
});

When you execute the existing code in the head, the clickMe div does not exist.


Try this:

<!DOCTYPE html>
<html>
<head>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function(){
    $(".clickMe").click(function() {
   alert('hi!');
   // Do other stuff
   });
});

</script>

</head>
<body>

<div class="clickMe">Click Me!</div>

</body>
</html>

You by comparing the differences you will find out the solution. Basically is this line: $(document).ready(function(){});


The DOM is not loaded yet when your JavaScript code is executed. You have to add the code to the .ready() callback. Then the code is executed when the browser parsed the HTML and jQuery can find the elements:

$(function() {

    $(".clickMe").click(function() {
       alert('hi!');
       // Do other stuff
    });

});

$(function(){..}); is shorthand for $(document).ready(function(){...}).

DEMO


The script is running before the .clickMe element even exists. Wrap your code in a dom ready callback:

$(function() {
    $(".clickMe").click(function() {
        alert('hi!');
        // Do other stuff
    });
});

Another solution would be moving your script below the element definition but using the dom ready event is much cleaner.


The issue is that you are attaching the event handler before the element is rendered to the DOM. Try this instead:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery.com/jquery-git.js"></script>

<script>
$(document).ready(function() {
    $(".clickMe").click(function() {
       alert('hi!');
       // Do other stuff
    });
});
</script>

</head>
<body>

<div class="clickMe">Click Me!</div>

</body>
</html>

The code passed into the $(document).ready function will be executed once the page is entirely loaded - so you can safely attach events to yet-to-be-created elements.

http://api.jquery.com/ready/


You're asking jquery to find your clickMe div before it exists. It's down lower on the page and the browser has not loaded it yet. A simple fix is to set up your click handler during the document ready event:

<script>
$(document).ready(function() {
    $(".clickMe").click(function() {
       alert('hi!');
       // Do other stuff  
    });
});
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消