Reputation: 14416
Given the following structure:
<ul>
<li><a href="example.com>1</a></li>
<li><a href="example.com>2</a></li>
<li><a href="example.com>3</a>
<ul>
<li><a href="example.com">3.1</a></li>
<li><a href="example.com">3.2</a>
<ul>
<li><a href="example.com">3.2.1</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="example.com>4</a></li>
</ul>
I'd like to auto remove or kill the link for 3
and 3.2
Basically any li that has children the link should be removed. What's the best way to go about this?
Upvotes: 2
Views: 1081
Reputation: 2009
using jQuery,
$('li > a').each(function() {
if ($(this).siblings().length)
$(this).remove();
});
Upvotes: 0
Reputation: 816512
If you don't want to keep the text:
$('li:has(ul) > a').remove();
If you want to keep the text, you could use .replaceWith
:
$('li:has(ul) > a').replaceWith(function() {
return $(this).text();
});
Upvotes: 7
Reputation: 146310
$("a").click(function(e){
if( $(this).parent().children('ul').length > 0 ){
e.preventDefault(); //prevent the link from being followed
$(this).remove(); //remove the a element
}
});
Upvotes: 0
Reputation: 22174
Even if I didn't tested, this should work:
$('li').filter(function(){ return $('ul', this).length ? this : ''; }).find('a:first').unwrap('<a></a>')
Upvotes: 1