admin管理员组

文章数量:1302410

How do I call a jquery function by just loading the page? For example, on one page,I have a paragraph and this jquery code here:

$(document).ready(function(){
  $("button").click(function(){
    $("p").hide(1000);
  });
});

How do I make the paragraph slowly fade away using jquery right after the page loads WITHOUT having any user input like clicking the button?

How do I call a jquery function by just loading the page? For example, on one page,I have a paragraph and this jquery code here:

$(document).ready(function(){
  $("button").click(function(){
    $("p").hide(1000);
  });
});

How do I make the paragraph slowly fade away using jquery right after the page loads WITHOUT having any user input like clicking the button?

Share Improve this question asked Jun 3, 2011 at 8:00 user701510user701510 5,77317 gold badges62 silver badges86 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Just a small warning.. $('p') will select all the paragraphs in the page.. use the selector more clearly..

to achieve the requirement, you can add this line as said above..

$("button").click();

or

$("button").trigger('click');

A much better way of wiring this is..

$(document).ready(function(){

  $("button").click(function(){
    $("p").hide(1000);
  }).trigger('click');

});

this will improve the performance by reducing the no.of search cycles.. :)

cheers

Can't you just call hide like that:

$(document).ready(function(){
    $("p").hide(1000);
});
$(document).ready(function(){
  $("button").click(function(){
    $("p").hide(1000);
  });

  $("button").click();

});

Just add that line.

just like this:

$(document).ready(function(){
    $("p").hide(1000);
});

本文标签: javascripthow to call jquery function without having to trigger selector eventStack Overflow