简体   繁体   中英

Javascript, remove class of DIV based on value of Link's innerHTML

I am looking for some help with Javascript. I have a piece of HTML as follows:

<div class="KeepShopping FloatRight GreenButton">
    <a href="http://www.xyz.com">Click here to keep shopping</a>
</div>

I am trying to remove class "GreenButton from the DIV once the link inside it has no text in the HTML, so the end result should look like this:

<div class="KeepShopping FloatRight">
    <a href="http://www.xyz.com"></a>
</div>

I have been unsuccessfully trying to get this with the following code, which runs at end of page load/refresh:

<script type="text/javascript">    
$(".KeepShopping").each(function() {
    if($("a", this).html == "") {
        $(this).removeClass("GreenButtonLge");
    }
});
</script>    

Any suggestions / ideas are really welcome! Thank in advance for any help!

html is a function, not a property. Write like this..

<script type="text/javascript">    
$(".KeepShopping").each(function() {
    if($("a", this).html() == "") {
        $(this).removeClass("GreenButtonLge");
    }
});
</script>

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