admin管理员组文章数量:1417538
Here's my script for loading a certain page via ajax.
$(function(){
// Keep a mapping of url-to-container for caching purposes.
var cache = {
// If url is '' (no fragment), display this div's content.
'': $('.bbq-default')
};
// Bind an event to window.onhashchange that, when the history state changes,
// gets the url from the hash and displays either our cached content or fetches
// new content to be displayed.
$(window).bind( 'hashchange', function(e) {
// Get the hash (fragment) as a string, with any leading # removed. Note that
// in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
var url = $.param.fragment();
// Remove .bbq-current class from any previously "current" link(s).
$( 'a.bbq-current' ).removeClass( 'bbq-current' );
// Hide any visible ajax content.
$( '.bbq-content' ).children( ':visible' ).hide();
// Add .bbq-current class to "current" nav link(s), only if url isn't empty.
url && $( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );
if ( cache[ url ] ) {
// Since the element is already in the cache, it doesn't need to be
// created, so instead of creating it again, let's just show it!
cache[ url ].show();
} else {
// Show "loading" content while AJAX content loads.
$( '.bbq-loading' ).show();
// Create container for this url's content and store a reference to it in
// the cache.
cache[ url ] = $( '<div class="bbq-item"/>' )
// Append the content container to the parent container.
.appendTo( '.bbq-content' )
// Load external content via AJAX. Note that in order to keep this
// example streamlined, only the content in .infobox is shown. You'll
// want to change this based on your needs.
.load( url, function(){
// Content loaded, hide "loading" content.
$( '.bbq-loading' ).hide();
});
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );
});
I am usng the JQuery BBQ plugin, now my question how can I get the only the div of the page that I want to load? not the whole page itself, any idea how?
Here's my script for loading a certain page via ajax.
$(function(){
// Keep a mapping of url-to-container for caching purposes.
var cache = {
// If url is '' (no fragment), display this div's content.
'': $('.bbq-default')
};
// Bind an event to window.onhashchange that, when the history state changes,
// gets the url from the hash and displays either our cached content or fetches
// new content to be displayed.
$(window).bind( 'hashchange', function(e) {
// Get the hash (fragment) as a string, with any leading # removed. Note that
// in jQuery 1.4, you should use e.fragment instead of $.param.fragment().
var url = $.param.fragment();
// Remove .bbq-current class from any previously "current" link(s).
$( 'a.bbq-current' ).removeClass( 'bbq-current' );
// Hide any visible ajax content.
$( '.bbq-content' ).children( ':visible' ).hide();
// Add .bbq-current class to "current" nav link(s), only if url isn't empty.
url && $( 'a[href="#' + url + '"]' ).addClass( 'bbq-current' );
if ( cache[ url ] ) {
// Since the element is already in the cache, it doesn't need to be
// created, so instead of creating it again, let's just show it!
cache[ url ].show();
} else {
// Show "loading" content while AJAX content loads.
$( '.bbq-loading' ).show();
// Create container for this url's content and store a reference to it in
// the cache.
cache[ url ] = $( '<div class="bbq-item"/>' )
// Append the content container to the parent container.
.appendTo( '.bbq-content' )
// Load external content via AJAX. Note that in order to keep this
// example streamlined, only the content in .infobox is shown. You'll
// want to change this based on your needs.
.load( url, function(){
// Content loaded, hide "loading" content.
$( '.bbq-loading' ).hide();
});
}
})
// Since the event is only triggered when the hash changes, we need to trigger
// the event now, to handle the hash the page may have loaded with.
$(window).trigger( 'hashchange' );
});
I am usng the JQuery BBQ plugin, now my question how can I get the only the div of the page that I want to load? not the whole page itself, any idea how?
Share Improve this question asked Dec 24, 2012 at 0:39 user962206user962206 16.2k65 gold badges185 silver badges273 bronze badges 5-
You can't load only one div, but you can
find
it in returned data. – zb' Commented Dec 24, 2012 at 0:43 -
@eicto actually you can and is part of the
load
method – charlietfl Commented Dec 24, 2012 at 0:50 - @charlietfl it is search through the page after loading.... – zb' Commented Dec 24, 2012 at 0:51
-
1
@eicto yes...full page is retrieved, but
load
will search for selector provided in argument without you manually callingfind
– charlietfl Commented Dec 24, 2012 at 0:52 -
I almost never use
load
method, so i answered usingload
therm – zb' Commented Dec 24, 2012 at 0:53
1 Answer
Reset to default 7load()
method allows for adding a selector after the actual file url to only load content that matches the selector.
For example:
$('#someDiv').load('myFile.php #contentDiv');/* note space before selector*/
This will retrive the full output of myFile.php
but only insert contentDiv
from that output into somediv
If selector is a class with multiple items it will retrieve them all
API reference: http://api.jquery./load/
See section titled Load Fragments
本文标签: javascriptHow to load a page fragment via Ajax with JQueryStack Overflow
版权声明:本文标题:javascript - How to load a page fragment via Ajax with JQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745273874a2651060.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论