Consider the following code.
$.each( links, function( i, link ) {
var $a = $( "<a/>" ).attr( "title", link.name )
.attr( "href", link.href )
.text( link.name );
$( "<li/>" ).append( $a ).appendTo( $ul );
});
<a/> element to the <li/> element. It does this by building the <a/>, placing it in the $a variable, then appending that to the <li/>. Here's where the wrap() function comes in handy.
$.each( links, function( i, link ) {
$( "<a/>" ).attr( "title", link.name )
.attr( "href", link.href )
.text( link.name )
.appendTo( $ul )
.wrap( "<li/>" );
});
$a variable to hold the element while we're building the <li/>. Instead, we append the <a/> to the <ul/>. Wait, what? Yes, appending the <a/> to the <ul/> let's us use wrap(), without breaking the chain. And there's no need to setup a temporary variable using this approach.
No comments :
Post a Comment