首页 文章

Tampermonkey .click()无效

提问于
浏览
0

我试图自动点击tampermonkey中的按钮,但由于某种原因代码没有执行 . 但是,如果我将代码放入控制台并运行它,它可以正常工作 .

这里是:

$(document).ready(function() {
    path = window.location.pathname;
    setTimeout(autoTraderReady, 10);
    $('#VehicleApplyButton').click();
});
<table id="VehicleApplyButton" class="x-btn va-apply-button x-btn-noicon x-column" cellspacing="0"><tbody class="x-btn-small x-btn-icon-small-left"><tr><td class="x-btn-tl"><i>&nbsp;</i></td><td class="x-btn-tc"></td><td class="x-btn-tr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-ml"><i>&nbsp;</i></td><td class="x-btn-mc"><em class=" x-unselectable" unselectable="on"><button class=" x-btn-text" id="ext-gen147" type="button">&nbsp;</button></em></td><td class="x-btn-mr"><i>&nbsp;</i></td></tr><tr><td class="x-btn-bl"><i>&nbsp;</i></td><td class="x-btn-bc"></td><td class="x-btn-br"><i>&nbsp;</i></td></tr></tbody></table>

按钮不动态切换,尝试在函数运行时发出警报,不提醒我 .

1 回答

  • 2

    鉴于您的代码:

    1. $(document).ready(function() {
    2.     path = window.location.pathname;
    3.     setTimeout(autoTraderReady, 10);
    4.     $('#VehicleApplyButton').click();
    5. });
    

    根据下面的评论,第4行的点击预计会触发从文档中其他位置的 .click 侦听器触发的AJAX请求 . 如果这个监听器存在于外部脚本中,我怀疑另一个监听器是否能及时捕获你正在触发的click事件 . 也就是说,它在你的点击已经被触发后开始收听 .

    $(document).ready 仅等待仅加载DOM,而不是外部脚本;尝试将第1行更改为 $(window).on('load', function(){...}); .

    如果失败,请尝试添加以下调试行:

    1. $(document).ready(function() {
    2.     console.log( $('#VehicleApplyButton') );
    3.     $('#VehicleApplyButton').click(function(e){ console.log( e ) } );
    4.     $('#VehicleApplyButton').click();
    5. });
    

    Line 2 - 确认#VehicleApplyButton存在

    Line 3 - 确认点击事件正在传播

    注意:我的初稿忽略了 jQuery.click() 解释为没有参数的 .trigger('click') 的快捷方式,而不是具有1-2个参数的监听器 .on('click',[data],handler) . 感谢礼貌的纠正,@ robertklep .

相关问题