我有一个按钮(超链接),其等效的HTML代码如下:
<span title="Enroll in Classes" class="SSSBUTTON_CONFIRMLINK">
<a class="SSSBUTTON_CONFIRMLINK" href="javascript:submitAction_win0(document.win0,'DERIVED_REGFRM1_LINK_ADD_ENRL$118$');" tabindex="202" ptlinktgt="pt_peoplecode" id="DERIVED_REGFRM1_LINK_ADD_ENRL$118$" name="DERIVED_REGFRM1_LINK_ADD_ENRL$118$">Proceed to Step 2 of 3</a>
</span>
我想在页面加载10秒后点击按钮,我使用以下代码:
// ==UserScript==
// @name Add to cart
// @namespace Class
// @description Script to add to cart
// @include http://go.oasis.com/add/?STRM=2141
// @version 1
// @grant none
// ==/UserScript==
setInterval (clickOnProceedButton, 10 * 1000)
function clickOnProceedButton () {
var targSubmit = $("#DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
targSubmit[0].dispatchEvent (clickEvent);
}
上述脚本无效,不会点击按钮。有人可以指出这里有什么问题吗?
编辑: 我在控制台中收到以下错误
ERROR: Execution of script 'Add to cart' failed! Object [object global] has no method 'clickOnProceedButton'
TypeError: Object [object global] has no method 'clickOnProceedButton'
at Object.eval (unknown source)), 25:14)
at Object.eval (unknown source)), 27:4)
at ag (unknown source), 190:4)
at K (unknown source), 190:46)
at o (unknown source), 456:2)
at U (unknown source), 460:85)
at R (unknown source), 229:40)
提前致谢!
答案 0 :(得分:1)
在谷歌浏览器中完美运作,但未在 IE8
中工作<强> HTML:强>
<a href="https://google.com" id="DERIVED_REGFRM1_LINK_ADD_ENRL$118$" >Proceed to Step 2 of 3</a>
<强> JS:强>
function clickOnProceedButton () {
var targSubmit = document.getElementById("DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
var clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent ('click', true, true);
targSubmit.dispatchEvent(clickEvent);
}
可能会帮到你。
答案 1 :(得分:1)
setInterval(clickOnProceedButton, 10 * 1000);
function clickOnProceedButton() {
var link = document.getElementById("DERIVED_REGFRM1_LINK_ADD_ENRL$118$");
if(link) {
link.click();
}
}