简体   繁体   中英

JQuery Click on a div which contains a specific text

I am trying to click on a div while contains a specific text.

This is where I am:

$( document ).ready(function() {

$('.myclass li.active .short-text'["value=thetext"]).click();


});

The code above is not doing it ... How can I get this to work?

You can use contains-selector

Select all elements that contain the specified text.

$('.myclass li.active .short-text:contains("someText")').click();

this is the jquery method of checking if something contains a text. i don't know if you can use it as a click function though

   $('.myclass li.active .short-text:contains("thetext")').click();

This should do what you like. On clicking the div, it checks the divs text contents, and in this case if it equates to "My Text", you'll get an alert.

DEMO

The key here is to use .text() to access the element.

Something like this should do the trick. Not sure if there is a better solution.

$(document).ready(function() {
    $("div").click(function() {
        if($(this).text() == "The text") {
            //Code here
        });
    });
});

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