【问题标题】:How to make a Userscript that Automatically clicks on a Button when it appears如何制作一个在按钮出现时自动点击按钮的用户脚本
【发布时间】:2014-03-11 13:44:52
【问题描述】:

我需要脚本点击的按钮是这样设置的:

<div id="closer" style="visibility: visible;">
    <input style="height:32px" type="button" onclick="javascript:showIt('hide');" value="Click here to return to AdventureQuest">
</div>

我想不通的是如何将按钮作为变量引用,因为按钮没有 id,getElementByID() 将不起作用。我认为为了引用按钮,我需要以某种方式引用 &lt;div&gt; 元素。

【问题讨论】:

  • 您希望在哪个事件上单击按钮?
  • 试试$("div#closer input")
  • 你的问题不清楚。请提供更多数据和样本。您可以使用任何方式 $('input[type="button"]','#closer') 访问您的按钮
  • 关于 DOM 更改事件,请查看以下问题的 apsillers awnser:stackoverflow.com/questions/2844565/… 当触发 DOM 更改事件时,通过 id 检查 div#closer 是否存在以及是否存在使用 'hide' 参数触发 unsafeWindow 对象上的 showIt() 函数,因为此函数似乎绑定到窗口对象。希望这可以帮助您获得一个基本的想法。如需更详细的帮助,我需要更多信息。

标签: javascript jquery button automation click


【解决方案1】:

您的问题令人困惑.. 您需要选择器来找到按钮吗? $('#closer&gt;input[type=button]') ...

在问题标题中你说自动点击按钮?触发点击它:

$('#closer>input[type=button]').trigger('click'); 

【讨论】:

    【解决方案2】:

    你可以使用

    getElementByTagName(input);
    

    这将为您返回页面上所有输入标签的数组。 如果这是唯一的输入标签,请使用

    getElementByTagName(input)[0];
    

    如果您使用 jQuery,您可以通过先获取 div 然后选择输入来更轻松地访问它。使用 .trigger() 触发动作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2016-07-22
      相关资源
      最近更新 更多