some questions in load jquery.ajax
function contentDisp()
{
$.ajax({
url : "a2.php",
success : function (data) {
$("#contentArea").html(data);
}
});
}
<input type="button" value="Click" onClick="contentDisp();"> <span style="color:blue;">
<textarea id="contentArea" rows="10" cols="50"></textarea>
I'm new learning jquery.ajax now. I found some tutorial on the web. these are some code for control jquery.ajax when click a button, then load the content from a2.php
to div#contentArea
. I have some questions:
whether the js code can add a
jQuery(document).ready(function()
if I want open the page, load thehtml(data)
at onece, not a click callback?whether jquery.ajax can load a div's content form
a2.php
, not a whole page? similar jquery.load$("#contentArea").load("a2.php #content");
.
Tha开发者_StackOverflow社区nks.
If you put your ajax call in the document ready it will run and load content immediately. This is effectively how the default tab on jQuery tabs works.
jquery.load is an abstraction from the full ajax function. you can do anything with .ajax that you can with .load
As a note, I don't like calling $.ajax() with all of it's parameters repeatedly, I've shown my pattern previously here: Showing Loading Image in Modal Popup Extender in Webservice ajax call
For your purpose, however, the following snipped would load the page on page load.
<script>
$(document).ready(function() {
$.ajax({
url: "a2.php"
,success: function(data) {
$("#contentArea").html(data);
}
});
});
</script>
<textarea id="contentArea" rows="10" cols="50"></textarea>
精彩评论