如何使用类单击

问题描述 投票:-4回答:1

我正在学习如何使用Jquery在tampermonkey(chrome)中编写脚本。我有一个问题,我想点击GO 1,然后点击GO 2.我尝试使用课程,但它不适合我。

这是网站代码的示例。

<div class="location">
            <a onclick="go_1.submit();" title="Go">
                <form action="/place" method="post" name="go_1">
                    <input type="hidden" name="id" value="1">
                </form>
                <img src="img/location/s/Go 1.jpg" height="40" width="50" alt="Place" One="" border="0">
            </a>
            </div>

<div class="location">
            <a onclick="go_2.submit();" title="Go 2">
                <form action="/place" method="post" name="go_2">
                    <input type="hidden" name="id" value="2">
                </form>
                <img src="img/location/s/Go 2.jpg" height="40" width="50" alt="Place2" Two="" border="0">
            </a>
            </div>

我尝试使用此功能,但它不起作用:(

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://website.xx/xx
// @grant        none
// @require      http://code.jquery.com/jquery-latest.js
// @run-at       document-end

// ==/UserScript==


$(function(){
    document.getElementsByClassName("go_1.submit")[0].click();
});



$(function(){
    document.getElementsByClassName("go_2.submit")[0].click();
});

我在这里寻找答案,但我一无所获。英语不是我的主要语言,所以对于错误感到抱歉。

javascript jquery tampermonkey
1个回答
1
投票

您发布的HTML中没有任何内容包含名为go_2submit的类。您可以通过使用其他属性(如标题)选择a标签来单击它们。

document.querySelector('[title="Go"]').click();
© www.soinside.com 2019 - 2024. All rights reserved.