【正文】
imple: find things, do stuff. We select elements from the document (via the DOM) using the jQuery function, aliased as $(). This handy function acts just like (), except that instead of only supporting IDs, it supports CSS selectors and some XPath selectors。 and, instead of returning one element, it can return an array of elements. Okay, so maybe a better description of $() is that it‘s like () on steroids. We then use functions to perform actions on our selections. For example, to append the text ―Hello World!‖ to all divs with the class 39。, then set the color to red, we‘d use the following code: $().append(Hello World!).css(color,red)。 $().css(color,red)。 Ideally, you39。39。 You can do the same with a separate file for $suffixes. $prefixes = array(39。,39。,39。,39。)。Web39。Push39。Media39。GUI39。 This selects a random element of each array on the fly echo $prefixes[rand(0,count($prefixes)1)] . is the new . $suffixes[rand(0,count($prefixes)1)]。generate39。 As you probably already know, if we were to select this element in CSS, the would identify that we were making our selection using the element‘s id attribute. You can use exactly the same syntax with jQuery. Therefore, to select the button with the id 39。 (which we gave it above), we can use the element expression generate. Also, be aware that this syntax defines our event handler as an anonymous function within the event itself. Mark Wubben‘s JavaScript Terminology page offers a great explanation of anonymous functions, if you‘d like to know more. We‘re going to use one of jQuery‘s higher level Ajax functions, load(). Let‘s assume that our generator script is saved as . Let‘s integrate it with our client side with the help of the load() function: $(generate).click(function(){ $(quote).load()。 That‘s it: three lines of code, and we have fully functioning Ajax random quote generator! Well, almost. The problem with JavaScript is that code that‘s not within a function is executed as soon as the browser reaches it during rendering — not once the page has finished rendering. As such, this code will try to attach to an element that has not yet loaded. Normally, we‘d use to deal with this issue. However, the limitation with that approach is that is called once everything has finished loading — images and all. We‘re not interested in waiting for those images — it‘s just the DOM that we want access to. Fortunately, jQuery has $(document).ready(), which, as its name suggests, is executed when the DOM is ready to be manipulated. The Complete Code Here‘s the plete code, including the $(document).ready wrapper and some basic HTML and CSS: html 5 head titleAjax with jQuery Example/title script type=text/JavaScript $(document).ready(function(){ $(generate).click(function(){ $(quote p).load()。 })。 height: 80px。 padding: 10px。 border: 1px solid black。