简体   繁体   中英

jQuery check if div contain text not working

I have such code:

$(document).ready(function () {
   var el = $('#solving'); 
   //var el = $('#solving:contains("Ожидайте появления новой капчи!...")');   
    setInterval(function(){
      console.log("Checking if el has text");
      if (el.text() == 'Wait! Ожидайте появления новой капчи!...'){
        console.log("Reloading");
      }
    },2000);
});

but in log's i see, that it doesn't work, i didn't see logger's Reloading even if page contain:

<div id="solving">
          Wait! Ожидайте появления новой капчи!...
        </div>

also i try:

$('#solving:contains("Wait! Ожидайте появления новой капчи!...")')

what wrong? How to check my div on present of some text?

Your existing code works 2 sec interval .I have changed to 400 ms.

$(document).ready(function () {
   var el = $('#solving'); 
   //var el = $('#solving:contains("Ожидайте появления новой капчи!...")');   
    setInterval(function(){
      console.log("Checking if el has text");
      if (el.text().indexOf("Wait!") != -1){
        console.log("Reloading");
      }
    }, 400);
});

You can do the following:

if ($('#solving:contains("Wait! Ожидайте появления новой капчи!...")').length > 0) {
  // found
}

See http://jsfiddle.net/8644K/1/ . Even if your selector contains :contains(...) , it's just a selector. That does not change the return value of $(<selector>) to a boolean, which still remains a list of potentially matched objects. Therefore, you can check how many of these matched objects could be found.

As the documentation ( http://api.jquery.com/contains-selector/ ) says, Select all elements that contain the specified text.

Use

el.text().trim()== 'Wait! Ожидайте появления новой капчи!...'

You have to strip the new line character as you started the text with next line to

Demo http://jsfiddle.net/f4PQM/

$(document).ready(function () {
   var el = $('#solving'); 
   //var el = $('#solving:contains("Ожидайте появления новой капчи!...")');   
    setInterval(function(){
      console.log("Checking if el has text" + el.text());
      if (el.text().trim() == 'Wait! Ожидайте появления новой капчи!...'){
        console.log("Reloading");
      }
    },2000);
});

Use trim() with el.text() because div text has whitespace.

  var el = $('#solving');
  //var el = $('#solving:contains("Ожидайте появления новой капчи!...")');   
    setInterval(function(){
      console.log("Checking if el has text");
      if (el.text().trim() == 'Wait! Ожидайте появления новой капчи!...'){
        console.log("Reloading");
      }
    },2000);

Here is the working fiddle : http://jsfiddle.net/J93mr/

Your problem is that your div contains spaces in the beginning of it's content , so you should delete spaces using $.trim() like this : DEMO FIDDLE

  if ($.trim(el.text()) == 'Wait! Ожидайте появления новой капчи!...'){
    console.log("Reloading");
  }

But solution using length() is better as suggested by @Remo :

if ($('#solving:contains("Wait! Ожидайте появления новой капчи!...")').length > 0) {
          console.log("Reloading");

}

http://jsfiddle.net/fPaxU/1/

el.text().trim()

All you need is a simple 'trim'

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