开发者

Find one <a> tag and remove text

开发者 https://www.devze.com 2023-04-13 08:15 出处:网络
I have the following code. http://jsfiddle.net/CtMmT/4/ Now i need to remove the text from thetag where you can find the \"Google must dissappear\" text

I have the following code.

http://jsfiddle.net/CtMmT/4/

Now i need to remove the text from the tag where you can find the "Google must dissappear" text

The output for that row must be

<a title="" href="http://www.google.com"></a>.

I tried to select the a tag , but i can't get only that 1 tag. I get always, 开发者_StackOverflow中文版all the a tags . I tried with the following each loops. But not with any result.

I can't set any class on that tag to control so that is not an option.

$('ul.menu li.dhtml-menu').each(function() {


}); 

$('ul.menu li.dhtml-menu a').each(function() {


}); 


If you're just looking to remove the inner text of the anchor tag, you'll need to loop through all the anchor tags in your div, and check their text() value.

jQuery:

$('#content a').each(function() {
    if ($(this).text() == 'Google Must disappear')
        $(this).text('');
});

Working example on jsFiddle.

Edit for OP's comment: Since you're looking to manipulate an element based on the structure, rather than the inner text, I strongly recommend reworking your markup so that it is valid first. As your markup is right now, nesting a li directly inside an li is causing Firefox to close the first li tag:

Find one <a> tag and remove text

I would change the markup first, rather than writing some jQuery to deal with this tag soup.


Is this what you're looking for?

$('ul.menu a').each(function() {
  if($(this).html() == "Google Must disappear")
      $(this).html("");
}); 


i still do not understand what you are talking about, but if you want select some node, you can specify it a id.

like:

<a href="http://google.com" id="googleLink">Google</a>

and select it by

 $("#googleLink")

otherwise, you can use the child selector in jQuery.


You can add a div around it and hide it.

http://jsfiddle.net/mR2Ts/

    <div id='content'>
<ul class="menu">
    <li id="dhtml_menu-759" class="dhtml-menu">
        <li class="leaf dhtml-menu-cloned-leaf">
            <a title="" href="http://www.google.com">Google</a>
        </li> 
        <div id="hide-this">
        <a title="" href="http://www.google.com">Google Must disappear</a>
            </div>
        <ul class="menu">
            <li id="dhtml_menu-760" class="dhtml-menu">
                <a href="http://www.microsoft.com">Microsoft</a>
            </li>
        </ul>
    </li>
</ul>
    <a href="#hide-this" class="close">Clicking here will hide the link</a>
</div>


$(function() {
$('a.close').click(function() {
    $($(this).attr('href')).slideUp();
    return false;
});

});

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号