最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论