我正在尝试自动点击按钮以在加载html页面时启动功能。我已经尝试了document.getElementById('watchButton').click
它似乎不起作用,按钮没有被点击。有什么建议?
<div class="content">
<div class="action-area ch30">
<button class="button dh" id="watchButton">Start Geolocation Watch</button>
<button class="button dh" id="refreshButton" >Refresh Geolocation</button>
</div>
javascript:
run:function() {
var that = this;
document.getElementById("watchButton").addEventListener("click", function() {
that._handleWatch.apply(that, arguments);
}, false);
document.getElementById("refreshButton").addEventListener("click", function() {
that._handleRefresh.apply(that, arguments);
}, false);
},
谢谢!
答案 0 :(得分:6)
我把它放在document.ready中(因此在DOM加载之前它不会触发)并使用jQuery语法:
$(function() {
$('#watchButton').click();
});
http://jsfiddle.net/isherwood/kVJVe/
这是使用jQuery语法的同一个小提琴:http://jsfiddle.net/isherwood/kVJVe/4
那说,为什么不直接命名你的功能并直接调用它?
答案 1 :(得分:2)
click()
而不是click
document.getElementById("watchButton").click();
你需要调用onload或函数运行后
答案 2 :(得分:0)
尝试触发器
<script>
$(document).ready(function() {
$("#watchButton").trigger('click');
});
</script>
答案 3 :(得分:0)
document.getElementById("studyOne").click();
$("#studyOne").trigger('click');
把它放在onload函数中。它对我有用。