简体   繁体   English

如何检查文档是否已准备好在JavaScript中?

[英]How to check if the document is ready in JavaScript?

I am developing a simulator to simulate user actions in a Web page. 我正在开发一个模拟器来模拟网页中的用户操作。 In a special case, there are a few number of buttons in a page clicking on each changes the content of the page. 在特殊情况下,页面上有几个按钮,每个按钮都会更改页面内容。 I programmatically click on each button and then want to extract the new content added to the page. 我以编程方式单击每个按钮,然后想要提取添加到页面的新内容。 If you have a look at: 如果你看看:

http://www.elementbars.com/Build-a-Bar-Service.aspx# http://www.elementbars.com/Build-a-Bar-Service.aspx#

you can find an example. 你可以找个例子。

My code is something like this: 我的代码是这样的:


for (var int i=0; i< buttonArray.length; i++){ // buttonArray is the array of buttons
    triggerClickEvent(buttonArray[i]);
    extractNewContent();
}

function triggerClickEvent (clickableElement) {
   jQuery(clickableElement).trigger('click');
}

Both triggerClickEvent and extractNewContent are properly working, but the problem is that after triggering the click event, the JavaScript engine should wait for a while to make sure that the new content is added, but it does not behave as expected. triggerClickEvent和extractNewContent都正常工作,但问题是在触发click事件后,JavaScript引擎应该等待一段时间以确保添加新内容,但它不会按预期运行。 For example, I noticed that all the buttons are clicked, but extractNewContent extracts content for the first button, meaning the these two functions are not working synchronously. 例如,我注意到单击了所有按钮,但extractNewContent为第一个按钮提取内容,这意味着这两个函数不能同步工作。 I used the setTimeout function, but since it does not block the execution, so could not resolve the problem. 我使用了setTimeout函数,但由于它不会阻止执行,因此无法解决问题。 I also used functions checking the state of the document, but not working as well. 我还使用函数检查文档的状态,但不能正常工作。

I would be grateful if you could please help me. 如果你能帮助我,我将不胜感激。

Thanks. 谢谢。

The easiest way to do it is with a toolkit like jQuery. 最简单的方法是使用像jQuery这样的工具包。 Something like this: 像这样的东西:

jQuery(document).ready(function() {
...
});

or the more concise 或者更简洁

jQuery(function() {

});

Part of the problem is that "ready" is not standardized across browsers. 问题的一部分是“准备好”并不是跨浏览器标准化的。 Toolkits like jQuery make up for that. 像jQuery这样的工具包可以弥补这一点。

You can pass parameters to trigger function one of them would be a callBack function extractNewContent in our case. 您可以将参数传递给触发器函数,在我们的例子中,其中一个函数将是一个callBack函数extractNewContent。 And call this function after all modifications are done in click event handler. 在单击事件处理程序中完成所有修改后调用此函数。 So the handler would be 所以处理程序将是

$(clickableElement).on('click', function(event, callback){
                       ...
                       all DOM modifications goes here
                       ...
                       if(typeof callback === "function")
                           callback();
                       }

and then trigger it with extractNewContent as parameter 然后使用extractNewContent作为参数触发它

$(clickableElement).trigger('click',extractNewContent);

The following code works: 以下代码有效:

for (var int i=0; i< buttonArray.length; i++){ // buttonArray is the array of buttons
  triggerClickEvent(buttonArray[i]);
  alert('balalalala'); // if you remove this, it won't work.
  extractNewContent();
}

function triggerClickEvent (clickableElement) {
 jQuery(clickableElement).trigger('click');
}

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

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