tags:

views:

321

answers:

2

Basically I want to append <span></span> inside the anchor tag but outside of "home". I guess it should be easy but I just started using jQuery.

So this:

<li class="home"><a href="index.php">home</a></li>

Should end up like this with Jquery:

<li class="home"><a href="index.php"><span>home</span></a></li>

My HTML:

<ul id="navigation"> 
   <li class="home"><a href="index.php">home</a></li>
   <li class="portfolio"><a href="portfolio.php">portfolio</a></li>
   <li class="about"><a href="about.php">about</a></li>
   <li class="contact"><a href="contact.php">contact</a></li>
  </ul>

My CSS:

ul#navigation {
 height: 20px;
 float: left;
 list-style: none;
 width: 100%;
}
ul#navigation li {
 float: left;
 margin: 0 4px 0 0;
}
ul#navigation li a {
 background: url(../images/tab.png);
 color: #C0C0C0;
 display: block;
 float: left;
 font-size: 14px;
 height: 20px;
 outline: none;
 padding-left: 10px;
 text-decoration: none;
}
ul#navigation li a:hover {
 background-position: 0 -20px;
 color: #666;
}
#home li.home a, #portfolio li.portfolio a{
 background-position: 0 -20px;
 color: #666;
}
ul#navigation span {
 background: url(../images/tab.png) 100% 0;
 display: block;
 line-height: 20px;
 padding-right: 20px;
}
ul#navigation li a:hover span {
 background-position: 100% -20px;
}
#home li.home span, #portfolio li.portfolio span {
 background-position: 100% -20px;
}

My JS:

$(document).ready(function() {
    $('li.home a').wrapInner('<span></span>')
});
+7  A: 
$("li.home a").wrapInner("<span></span>")

Somehow StackOverflow clears my markup. There should be<span></span> inside the 'wrap' call.

naivists
Sorry but that wraps the span tags around the anchor tags but not inside them
janoChen
Sorry, misunderstood the question. Then you need wrapInner() , see http://docs.jquery.com/Manipulation/wrapInner#html
naivists
@jano - wrapInner() will do what you want.
womp
Thanks it worked! but its a bit weird the css properties for the span tags that are added with jQuery are not being applied correctly is this normal?
janoChen
no, this is not normal, there is something else about it. Can you post the CSS code you're using?
naivists
Oh never mind. I found out that it was just applied to li.home and not to the others. Thanks man!
janoChen
+1  A: 

Try this:

$('.home a').each(function(){
    var content = $(this).html();
    $(this).html('<span>'+content+'</span>');
});
GeekTantra
it almost worked but it rendered like this: "<span>[object Window]</span>"
janoChen