开发者

What's wrong with this jQuery example?

The following jQuery example should put some text into the div, but it doesn't. I tried Firefox, Google Chrome and Internet Explorer.

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script>
<script language="javascript">

$(window).load(function() {

  $('adiv').html('<p>hello world</p>');
  alert('done');

});
</script>
</head>

<body>
<div id="adiv">
</div>
</body>

<开发者_如何学运维;/html>

Sorry, this might be stupid, but I'm stuck.


change $('adiv').html('<p>hello world</p>');to

$('#adiv').html('<p>hello world</p>');


You're not actually selecting anything in your select function

Directly after your $( opening, you need to use a CSS3 valid selector. Just a string won't select anything unless it's a HTML element (table, div, h2)

You need to preface it with a . or a # to signal either a class or ID name.


$('adiv') should be $('#adiv').

Unlike Prototype, in jQuery you specify a CSS selector, not just a string that is implicitly inferred to be an ID. I find myself forgetting this from time to time.


As e-turhan already mentioned you need # in front of adiv in your $() otherwise this is not a id selector. Also it's always better to call these .load() events inside the .ready() jQuery event whose famous shortcut is $(function() { //execute when DOM is ready }); . In your case:

$(function(){

$(window).load(
 function(){
   $('#adiv').html('<p>hello world</p>');   
 }

);


<html> <head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" language="javascript"></script> <script language="javascript">  $(window).load(function() {    $('#adiv').html('<p>hello world</p>');   alert('done');  }); </script> </head>  <body> <div id="adiv"> </div> </body>  </html>

Paste this code instead ur query

It Will Work


Try $(document).ready(function()... instead of $(window).load(function()...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜