简体   繁体   中英

Making a button not appear if an aria-label appears within the div

Within this else statement, I'm trying to say that if there is an aria-label whose value is "Comment" within the uCW div, don't add the button. Otherwise, add the button. Nothing is appearing.

        else {
            if ($(uCW).find([aria-label=="Comment"]))
                {rollback}
            else
                {more.before(button);}
            }

This is the aria-label I'm trying to NOT add buttons to (it's inside uCW and within a lot of divs)

<div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1">

You can check inside uCW has label comment as

var exist = $(".uCW [aria-label='Comment']").length;
    if( exist == 0 )
    {
      $(".uCW").append("<button>Test</button>");
    }

 var exist = $(".uCW [aria-label='Comment']").length; if( exist == 0 ) { $(".uCW").append("<button>Test</button>"); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="uCW"> <div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1"> </div>

You have to access the attribute using a string . Like this $("[meta-tag='value']");

I'll amend my answer when you post code that will be more specific. :)

 const aria = $("[aria-label='Comment']"); console.log(aria);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="l9j0dhe7 ecm0bbzt hv4rvrfc qt6c0cv9 dati1w0a lzcic4wl btwxx1t3 j83agx80" aria-label="Comment" role="article" tabindex="-1"> </div> <div class="not"></div>

You are using == instead the = on a selector. Also you must add quotes in your find selectors. You can do the following in Jquery:

$('.your-div').find('[aria-label="Comment"]');

Then, you can check if they were found using:

if ($('.your-div').find('[aria-label="Comment"]').length > 0) {
  // aria label = Comment found
} else {
  // ...
};

If you assign this condition to a const, your code can be more semantic:

const ariaLabelComment = $('.your-div').find('[aria-label="Comment"]').length > 0;

if (ariaLabelComment) {
  // aria label = Comment found
} else {
  // ...
};

In Pure javasript

const uCW = document.querySelector('.your-ucw-div-selector');

const ariaLabelComment = uCw.querySelectorAll('[aria-label="Comment"]').length > 0;

if (ariaLabelComment) {
  // found
} else {
  // ...
}

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