jquery - Show javascript alert and do other actions? - Stack Overflow

Here's my code and nothing is happening:<!DOCTYPE html><html><head><script

Here's my code and nothing is happening:

<!DOCTYPE html>
<html>
<head>

<script src=".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!

Here's my code and nothing is happening:

<!DOCTYPE html>
<html>
<head>

<script src="http://code.jquery./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!

Share Improve this question asked Apr 27, 2011 at 21:37 iosfreakiosfreak 5,23811 gold badges62 silver badges102 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 5

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./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 paring 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./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./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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745266712a4619500.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信