简体   繁体   中英

How to remove text from a Tag

If an element has class active I want to remove only text from the tag. I tried it with $(selector).remove(selector) but a problem is I need selector to remove something...

<li class="active">
    <a href="profil-anzeigenverwaltung.php">
        <i class="fas fa-briefcase"></i>
        Anzeigenverwaltung
    </a>
</li>
<li>
    <a href="profil-anzeige-erstellen.php">
        <i class="fas fa-align-center"></i>
         Anzeige erstellen
    </a>
</li>
<li>
    <a href="profil-refrenzen.php">
        <i class="fas fa-anchor"></i>
         Referenzen
    </a>
</li>

You can do it with CSS only:

 .active a span { display: none; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <li class="active"> <a href="profil-anzeigenverwaltung.php"> <i class="fas fa-briefcase"></i> <span>Anzeigenverwaltung</span> </a> </li> <li> <a href="profil-anzeige-erstellen.php"> <i class="fas fa-align-center"></i> <span>Anzeige erstellen</span> </a> </li> <li> <a href="profil-refrenzen.php"> <i class="fas fa-anchor"></i> <span>Referenzen</span> </a> </li>

Hope that's what you need. ;)

The jQuery answer to remove text from within the desired tag is set the innerHTML value to the empty string. This may be accomplished in more than one way but here is one:

$('li.active a span').html('');

As Anna_B said you can (and in most cases probably should ) do it with CSS. The CSS may also be implemented differently, but in sticking with jQuery you could toggle classes, one class having the text visible and the other not.

What you must consider with CSS is the method of hiding elements affects the document flow. Discussion about this on SO .

eg display:none removes the element from the flow whereas visibility:hidden does not. The latter has unintended affects unless you are aware of and desire these aforementioned flow dynamics.

Regarding the hiding of text with jQuery here is a discussion and answers .

简单地用js

document.querySelector("li.active a").innerText=''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM