我无法单击具有api.edit(stuff)函数的`<a>` tag and no href attribute

时间:2018-07-17 10:57:58

标签: javascript jquery jquery-selectors userscripts

I am making a bot that locates a page and automatically finds an "href" like so <a class="batteekh"></a> what I want is just to click this hyperlink in any way I can.

After selecting my hyperlink $(".batteekh").attr("style","background-color:red") I changed the background-color to red and it works but what is happening is that I can't click on that link that has no href attribute.

I've searched a lot and tried several statements but none works:

  • $(".batteekh").trigger("click");
  • $(".batteekh").click()
  • $(".batteekh").get(0).click()

Thanks in advance!

I am trying to auto vote up a question in stackoverlow

EDIT:

// ==UserScript==
// @name         Stackoverflow Voter
// @namespace    http://tampermonkey.net/
// @version      0.1
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @description  try to take over the world!
// @author       You
// @match        https://stackoverflow.com/questions/*
// @grant        none
// ==/UserScript==

$(".vote-up-off").attr("style","background-color:red")[0].click();

NOTE : I am using this for learning purpose only

3 个答案:

答案 0 :(得分:2)

我认为$(".batteekh")[0].click()应该会根据您的情况进行点击。

答案 1 :(得分:2)

好吧,通常,href标记定义了单击后浏览器会将您带到的位置,因此,如果没有,则什么也不会发生。但是,大多数人会将<a>...</a>与点击事件一起使用,这似乎与您要尝试的事件相同。如果您触发$(".batteekh").click()却没有任何反应,仅表示按钮上没有单击事件,或者按钮上没有单击事件,则无济于事。

因此,首先要确定您是否首先将click事件附加到了按钮上?例如以下是我如何定义按钮的点击事件的方法:

$(".batteekh")
.unbind("click")
.bind("click", function ()
{
   alert('Got here first!!');
});

例如,您可以在萤火虫(Firefox)中放入$(".batteekh")并按Enter键,如果您调查对象,则可以看到该按钮上附加了哪些事件...

答案 2 :(得分:1)

这是一个有效的示例: JsFiddle

<a class="batteekh" style="cursor: pointer;">Link 1</a>

脚本

<script>
    $('.batteekh').click(function(){
    alert("You clicked me?");
});

如果您有多个链接:

$('.batteekh').click(function(){
  $(this).click(function(){
    alert("You clicked me?");
  });
});