简体   繁体   English

jQuery单击包含特定文本的div

[英]JQuery Click on a div which contains a specific text

I am trying to click on a div while contains a specific text. 我尝试在包含特定文本的同时单击div。

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. 这是检查某些内容是否包含文本的jquery方法。 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. 单击div时,它将检查div的文本内容,在这种情况下,如果它等于“我的文本”,则会收到警报。

DEMO DEMO

The key here is to use .text() to access the element. 此处的关键是使用.text()访问元素。

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
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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