简体   繁体   English

如何等待/检查元素是否存在

[英]How to wait / check if elements exist

I was wondering is there a way for me to check if element exist then do a function then repeat but with next id. 我想知道是否有一种方法可以检查元素是否存在,然后执行一个功能,然后重复但带有下一个id。

example: if log2643673 exist do function then check or wait for log2643674 exist and if exist then do function 示例:如果log2643673存在,则执行功能,然后检查或等待log2643674存在,如果存在,则执行功能

if it dose not exist keep checking into it exist and do function and then find next element exist 如果不存在,请继续检查是否存在并运行,然后找到下一个元素

 var checkExist = setInterval(function() { if ($('#log2643673').length) { console.log("it exists!"); } }, 1000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li id="log2643670" data-log="2643670" class="logs">Text here</li> <li id="log2643671" data-log="2643671" class="logs">Text here</li> <li id="log2643672" data-log="2643672" class="logs">Text here</li> <li id="log2643673" data-log="2643673" class="logs">Text here</li> 

sorry for my bad english 对不起,我的英语不好

Try this 尝试这个

$(function() {
    var logIdStart = 2643670; // the start log id
    // timer to check if element exists
    var checkExist = setInterval(function() {
       if ($("#log" + logIdStart ).length) {
          console.log("it exists!");
          logIdStart++; // update id only if element is found             
       }
    }, 1000 /* change checking frequency if needed*/ );

});

Play here 在这里玩

 $(function() { var logIdStart = 2643670; // the start log id $('button').on('click',function(){ $("body").append('<li id="log'+logIdStart+'">log'+logIdStart+'</li>'); }); // timer to check if element exists var checkExist = setInterval(function() { if ($("#log" + logIdStart ).length) { $("body").append("log" + logIdStart + " exists!<br />"); logIdStart++; } }, 1000 /* change checking frequency if needed*/ ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li id="log2643670" data-log="2643670" class="logs">Text here</li> <li id="log2643671" data-log="2643671" class="logs">Text here</li> <li id="log2643672" data-log="2643672" class="logs">Text here</li> <li id="log2643673" data-log="2643673" class="logs">Text here</li> <div> <button>Add Input</button> </div> 

Evaluate if the length of the element is greater than cero. 评估元素的长度是否大于cero。

var string_part = '#log';
var initial_number = 2643670;
var checkExist = setInterval(function() {
   var element = $(string_part + initial_number);
   if (element.length > 0) {
      console.log("it exists!");
      initial_number ++;
   }
}, 1000);

In case you have random IDs and need to check element is exists or not then you can do this : 如果您有随机ID并且需要检查元素是否存在,则可以执行以下操作:

 $(function() { var myListOfIds=$('li'); //Hold all the li tags. var cnt=0; // we will use it in array iterate. // timer to check if element exists var checkExistInterval = setInterval(function() { if(cnt== (myListOfIds.length)){ console.log(" Yeah, clear the interval,we have done it :)") clearInterval(checkExistInterval); } else if ($("#" + myListOfIds[cnt].id ).length) { console.log(myListOfIds[cnt].id +" Yes, I am in..."); cnt++; } }, 1000 /* Lets change checking frequency if needed*/ ); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <li id="log2643670" data-log="2643670" class="logs">Text here</li> <li id="log2643671" data-log="2643671" class="logs">Text here</li> <li id="log2643672" data-log="2643672" class="logs">Text here</li> <li id="log2643673" data-log="2643673" class="logs">Text here</li> <li id="IAmRandom" data-log="2643673" class="logs">Text here</li> <div> </div> 

Assuming that you know the starting id of your elements which you are trying to find and they come in numeric order. 假设您知道要查找的元素的起始ID,并且它们以数字顺序排列。

You can use pure Javascript to do this 您可以使用纯Javascript执行此操作

let counter = 2643670;
let checkExist = setInterval(function() {
   const id = 'log' + counter;
   const element = document.getElementById(id);
   if (!!element) {
      // Do you function call here
      counter++;
   }
}, 1000);

There you go. 妳去 :) :)

 function checkExist(startID) {
     setInterval(() => {
       if (document.getElementById(startID)) {
         console.log(`ID ${startID++} exists`);
       }
     } ,1000)
 }

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

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