简体   繁体   English

单击带有Javascript的网页中的按钮

[英]Click button in webpage with Javascript

Currently, I'm doing an application and I need login to this one particular website. 目前,我正在做一个应用程序,我需要登录到该特定网站。 Basically, I need to make this button click using a javascript but I have no idea how to. 基本上,我需要使用javascript来使此按钮单击,但是我不知道该怎么做。

This is the button code I retrieved from the website: 这是我从网站上检索到的按钮代码:

<button type="submit" class="uk-width-1-1 uk-button uk-button-primary uk-button-large">Sign in</button>

Thank you for your help in advance 提前谢谢你的帮助

You can click on the button programmatically, in case of pure javascript : 如果使用纯javascript,则可以通过编程方式单击按钮:

(function(){document.querySelector('button[type="submit"]').click();})()

In case of Android JS Interface : 如果是Android JS接口:

webView.loadUrl("javascript:(function(){"+
    "l=document.querySelector('button[type=\"submit\"]');"+
    "e=document.createEvent('HTMLEvents');"+
    "e.initEvent('click',true,true);"+
    "l.dispatchEvent(e);"+
    "})()");

The general way to have button click events is to use something like jQuery. 具有按钮单击事件的一般方法是使用类似jQuery的东西。 For example lets set the button to have an ID 例如,让我们将按钮设置为具有ID

<button id="my-button" type="submit" class="uk-width-1-1 uk-button uk-button primary uk-button-large">Sign in</button>

Then we would apply a click event to it with jQuery 然后我们将使用jQuery将click事件应用于它

$('#my-button').click(function(){alert('button has been clicked');})

Instead of the alert you could put whatever code you want in there :D 除了警告,您可以在其中放置任何所需的代码:D

Example: Codepen example 示例: Codepen示例

EDIT: Example for a class: Example for a class (Note that we use a "." instead of "#" in the call) 编辑:类的示例 :类的示例 (请注意,我们在调用中使用“。”而不是“#”)

-Kyle 凯尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM