|
Tutorials:Getting Started with jQuery - jQuery JavaScript Library
docs.jquery.com/Tutorials:Getting_Started_with_jQu... More often than selecting anchors by name, you might need to select anchors by their "href" attribute. This can be a problem as browsers behave quite inconsistently when returning what they think the "href" value is (Note: This problem was fixed recently in jQuery, available in any versions after 1.1.1). To match only a part of the value, we can use the contains select "*=" instead of an equals ("="): $(document).ready(function() {
$("a[@href*=/content/gallery]").click(function() {
// do something with all links that point somewhere to /content/gallery
});
});Tutorials:Getting Started with jQuery - jQuery JavaScript Library
docs.jquery.com/Tutorials:Getting_Started_with_jQu... Find me: Using selectors and eventsjQuery provides two approaches to select elements. The first uses a combination of CSS and XPath selectors passed as a string to the jQuery constructor (eg. To try some of these selectors, we select and modify the first ordered list in our starterkit. To get started, we want to select the list itself. The list has an ID "orderedlist". In classic JavaScript, you could select it by using $(document).ready(function() {
$("#orderedlist").addClass("red");
});
The starterkit provides a stylesheet with a class "red" that simply adds a red background. Therefore, when you reload the page in your browser, you should see that the first ordered list has a red background. The second list is not modified. Now lets add some more classes to the child elements of this list. $(document).ready(function() {
$("#orderedlist > li").addClass("blue");
});
This selects all child Now for something a little more sophisticated: We want to add and remove the class when the user hovers the li element, but only on the last element in the list. $(document).ready(function() {
$("#orderedlist li:last").hover(function() {
$(this).addClass("green");
},function(){
$(this).removeClass("green");
});
});Release:jQuery 1.2/Ajax - jQuery JavaScript Library
docs.jquery.com/Release:jQuery_1.2/Ajax#Cross-Doma... Cross-Domain getJSON (using JSONP)JSONP is a technique that allows you to transfer JSON data across multiple domains. jQuery now supports JSONP natively - if you attempt to load JSON (via $.getJSON or $.ajax) from a remote URL then an extra callback will be provided for the server to interpret. Additionally, if the server requires a special field for specifying your own callback name you can provide it by having a "=?" in your query string.
Loads the four most recent cat pictures from the Flickr JSONP API. $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images")
.wrap("<a href='" + item.link + "'></a>");
if ( i == 3 ) return false;
});
});
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?tags=cat&tagmode=any&format=json&jsoncallback=?", function(data){
$.each(data.items, function(i,item){
$("<img/>").attr("src", item.media.m).appendTo("#images")
.wrap("<a href='" + item.link + "'></a>");
if ( i == 3 ) return false;
});
});
});
</script>
<style>img{ height: 100px; float: left; }</style>
</head>
<body>
<div id="images"></div>
</body>
</html>JSON for jQuery
Michael Geary | Wed, 2006-01-25 00:10
category
Update 2007-09-13: As of version 1.2, the jQuery core now supports cross-domain JSONP downloads as part of the native Ajax support. I suggest you use this support instead of the plugin. jQuery is a nifty new JavaScript library by John Resig. It features a Unlike Prototype, jQuery doesn’t mess around with built-in JavaScript objects. It’s new—too new to have a version number!—but I’ve been writing some code with it and enjoying it. jQuery provides an easy way to write plugin methods to extend the function doJson( json ) {
// handle the json object here}
You can of course use an anonymous function if you prefer:
$('#test').json( url, function(json) { // handle the json object here
});Or, using jQuery’s method chaining, you can combine calls like this code which displays a “Loading…” message when it starts loading the JSON resource: To install the plugin, simply paste this code into a .js file and load it after loading jquery.js: // JSON for jQuery by Michael Geary
// See http://mg.to/2006/01/25/json-for-jquery// Free beer and free speech. Enjoy!
$.json = { callbacks: {} };
$.fn.json = function( url, callback ) {
var _$_ = this; load( url.replace( /{callback}/, name(callback) ) );
return this; var id = (new Date).getTime();
var name = 'json_' + id; delete $.json.callbacks[id];
eval( 'delete ' + name ); _$_.each( function() { callback(json); } );
}; return name;
} var script = document.createElement( 'script' );
script.type = 'text/javascript'; script.src = url;
$('head',document).append( script ); }
};This adds a The second argument is the callback function itself. When the JSON resource finishes loading, this function will be called with a single argument, the JSON object itself. Inside the callback function, The callback function is required, so this code won’t work with plain JSON APIs like del.icio.us that don’t let you specify a callback function. This would be easy enough to fix; I didn’t need it for the code I was writing, and didn’t think of it until just now. :-) The code goes to a bit of extra work to create both an array entry and a unique global name for each callback. The global name is what is substituted into the {callback} part of the URL. It uses this name instead of the array reference to ensure compatibility with any JSON APIs that don’t allow special characters in the callback name. In fact, in the current code the |