Java Reference
In-Depth Information
It's important to note that since this method returns true or false , you cannot chain any more
methods after calling it.
jQuery makes other types of DOM manipulation easy, as you'll see by creating, adding, and removing
objects from the DOM.
Creating, Appending, and Removing Elements
Think back to Chapter 12 and how you create and append elements to the page. The following code will
refresh your memory:
function window_onload()
{
var a = document.createElement(“a”);
a.id = “myLink”;
a.setAttribute(“href”, “http://jquery.com”);
a.setAttribute(“title”, “jQuery's Website”);
var text = document.createTextNode(“Click to go to jQuery's website”);
a.appendChild(text);
document.body.appendChild(a);
}
onload = window_onload;
This code defi nes the window_onload() function, which is called when the browser completely loads
the page. When window_onload() executes, it creates an <a/> element, assigns it an id , and sets the
href and title attributes. Then you create a text node and assign the object to the text variable. Finally,
you append the text node to the <a/> element, and then append the <a/> element to the document's
<body/> element.
Creating Elements
There's technically nothing wrong with this code; it is standard DOM element creation, population, and
insertion. However, it is rather long and verbose. You can do the same thing with less typing with jQuery,
and the following code shows you how:
function document_ready()
{
var a = $(document.createElement(“a”));
$(document.body).append
(
a.attr(“id”, “myLink”)
.attr(“href”, “http://jquery.com”)
.attr(“title”, “jQuery's Website”)
.text(“Click here to go to jQuery's website.”)
);
}
$(document).ready(document_ready);
Search WWH ::




Custom Search