Button元素在第一次调用时未被检索
Button element is not retrieved at the first call
我在JavaScript中编写脚本时遇到了一个奇怪的事情,我不完全确定我的理解方式是否正确。基本上,我想点击一个按钮,它会弹出另一个按钮,如果第二个按钮没有被第一个按钮弹出,我需要退出循环。
var intervalID = window.setInterval(myfunc,1000);
function t(){
console.log('quit');
clearInterval(intervalID);
}
function myfunc(){
//first button
document.getElementsByClassName('hit')[0].click();
//try to retrieve the second one
var el = document.getElementsByClassName('hit_ok');
console.log(el.length);
//if it exists click it
if (el.length == 1){
el[0].click();
//otherwise exit
} else {
console.log('ready to quit');
window.setTimeout(t,50);
}
}
我的问题是第一个实例在if语句中总是返回0我还尝试了以下操作:
function myfunc(){
document.getElementsByClassName('hit')[0].click();
var el = document.getElementsByClassName('hit_ok');
console.log(el);
if (el != null){
el[0].click();
} else {
console.log('ready to quit');
window.setTimeout(t,50);
}
}
实际上它返回:
[] -> length: 0__proto__: HTMLCollection
VM3642:1 Uncaught TypeError: Cannot read property 'click' of undefined
代替:[span.hit_ok]
这意味着第一次它不能检索按钮。显然,第二个按钮在那里,因为第一个按钮被按下了。
HTML代码://first button
<div class="try">
<input type="hidden" id="isHit" value="">
<a href="javascript:void(0);" class="hit">Try</a>
</div>
//second button
<div class="msgbox_button">
<span class="hit_ok" onclick="remove();">OK</span>
</div>
任何想法?
认为,
你做了什么:
intervalID=window.setInterval();
这是有效的js,但脚本是在页面加载之前启动的,这意味着DOM当时没有创建。
怎么做:
window.onload=function(){
var intervalID=window.Interval(...);
};
在页面加载完毕后开始循环
相关文章:
- *简单*解析云代码查询失败”;TypeError:无法调用方法'获取'未定义的“检索该信息”;
- 调用c#函数从传递javascript变量的javascript中检索数据
- 在用户提交表单后显示数据(从API调用检索),而不刷新页面
- 从api调用中检索to ng选项的值
- 无法从 ajax 调用中检索谷歌地图标记坐标
- 防止单击和检索函数调用的参数
- 从Flash AS3 Android应用程序调用Java脚本函数以检索设备ID和序列号
- 对从字典 JavaScript 对象检索的对象调用方法
- 从调用函数中检索回调值.如何
- 使用两个 Ajax 调用来发布然后检索数据
- 在颜色框调用时检索隐藏字段值
- 客户端 JavaScript 和/或 JavaScript 代码,使用 jquery 调用 WCF 服务检索 Syste
- 如何检索 java 设置并迭代来自 ajax 调用响应的值
- 当从客户端从ajax调用检索时,我在服务器上的日期时间增加了12.30小时
- Ajax调用工作正常,但php文件在从对象检索属性时出错
- 在Phonegap中的AJAX REST服务调用期间未正确检索值
- 在用户返回页面后,显示AJAX调用检索到的相同数据
- 如何将一些指定的时间设置为ajax调用,这样如果请求正在花费时间检索数据,它应该来到FAIL块
- 如何使用JQueryAjax调用从web方法发送和检索数据
- 如何对从CouchDB检索到的文档调用函数