简体   繁体   English

通过类名单击带有JavaScript / Jquery的按钮/ href

[英]Clicking a button/href with JavaScript/Jquery by class name

I'm trying to click a button that brings up an edit screen on the same page. 我试图单击一个在同一页面上显示一个编辑屏幕的按钮。

Eg. 例如。 Click "edit user", edit screen pops up on the same page without redirecting, change name, save. 点击“编辑用户”,编辑页面弹出,无需重定向,更改名称,保存。 This button does not redirect to a new page. 此按钮不会重定向到新页面。

The code for this button is the following: 该按钮的代码如下:

<a href="javascript:;" data-id="29508" class="btn btn-xs btn-inverse btn-modify"><i class="fa fa-pencil"></i> Edit</a>

For some reason I can click every other button on this website that's in the same exact form but I can't click this one. 由于某些原因,我可以单击该网站上具有相同格式的所有其他按钮,但不能单击该按钮。 There are no IDs at all so calling by the class name is the only other method I know. 根本没有ID,所以我知道唯一通过类名称进行调用的方法。

This is what I've tried: 这是我尝试过的:

setTimeout(function() {
   document.getElementsByClassName(" btn btn-xs btn-inverse btn-modify")[0].click();
}, 3000);

I tried using some Jquery instead as well but no luck. 我也尝试使用一些Jquery,但是没有运气。 Am I doing something wrong or is this all that I can really do? 我是在做错什么,还是我能做的就是全部? The other buttons I clicked either redirected me to a different site or just brought up some information on the same screen. 我单击的其他按钮将我重定向到另一个站点,或者只是在同一屏幕上显示了一些信息。

Thanks in advance. 提前致谢。

Edit: 编辑:

It seems that when I try iterating through the different buttons, who all have similar starting class names, I can iterate and click every button except for the edit button. 看来,当我尝试遍历所有具有相似起始类名称的不同按钮时,我可以迭代并单击除编辑按钮以外的所有按钮。 So it's safe to assume that this isn't an issue with the code, so thank you everyone for the help and suggestions. 因此可以安全地假设这不是代码问题,因此感谢大家的帮助和建议。

Edit #2: 编辑#2:

Here is the code for all three buttons: 这是所有三个按钮的代码:

 <div class="btn-group"><a href="https://google.com" target="_blank" class="btn btn-xs btn-inverse"><i class="stm stm-goog"></i>&nbsp;&nbsp</a>

<a href="javascript:;" data-id="29508" class="btn btn-xs btn-inverse btn-modify"><i class="fa fa-pencil"></i> Edit</a>

<a href="javascript:;" data-page-modal="https://*randomwebsite*.com/manage/stats/301&amp;q=11" class="btn btn-xs btn-inverse"><i class="fa fa-bar-chart"></i></a><button type="submit" class="btn btn-xs btn-danger btn-submit"><i class="fa fa-trash-o"></i></button></div>

If you want to fetch the element having all the class, try this: 如果要获取具有所有类的元素,请尝试以下操作:

JQuery JQuery的

$(".btn.btn-xs.btn-inverse.btn-modify")

Plain JavaScript 纯JavaScript

document.querySelectorAll(".btn.btn-xs.btn-inverse.btn-modify")

Giving a space in between a class names in a css like selector meant that (next one) is a nested element in any depth. 在类似css like选择器中的类名之间留一个space ,意味着(下一个)是任何深度的嵌套元素。 use . 使用. to indicate its a class, and write them together (without a space ) you are searching for elements having all the lasses. 要指示其类,并一起编写它们(不带space ),您正在搜索具有所有小类的元素。 And getElementsByClassName is not a css like selector, its can take only a class name and all those element having that class. 而且getElementsByClassName不是css like选择器那样的css like ,它只能采用类名,而所有具有该类的元素都可以。

Because you are trying to select the element by its class you need to loop through the elements. 因为您尝试按元素类选择元素,所以需要遍历元素。 So if you wanna use jQuery you would do like so: 因此,如果您想使用jQuery,则可以这样做:

$(".btn-modify").each(function(){

    $(this).click(function(){

    your code here

    });

});

When searching for elements by class, it's better to use: 当按类搜索元素时,最好使用:

document.querySelector();  // Finds first matching element only

and

document.querySelectorAll(); // Finds all matching elements

instead of document.getElementsByClassName() as this returns a "live node list" and hinder performance. 而不是document.getElementsByClassName()因为这会返回“活动节点列表”并影响性能。

When searching for classes with these methods, remember to include the dot ( . ) to signify classes and when multiple classes are used, do not include spaces. 使用这些方法搜索类时,请记住包括点号( . )以表示类,并且在使用多个类时,请勿包含空格。 The spaces are only used when setting multiple classes. 仅在设置多个类时才使用空格。

Also, if there is only one class that is unique to the element you wish to find, you only need to search on that one class. 此外,如果只有一个类别是您要查找的元素所独有的,则只需搜索该类别。

Lastly, only use a elements for navigation. 最后,只用a导航元素。 If you simply need something to click, just about any object can have a click event handler. 如果您只需要单击某些内容,则几乎任何对象都可以具有click事件处理程序。

 var edit = document.querySelector(".btn-modify"); edit.addEventListener("click", function(){ console.log("clicked")}); setTimeout(function() { edit.click(); }, 3000); // Addtional test var edit = document.querySelectorAll(".btn.btn-xs.btn-inverse")[1]; edit.addEventListener("mouseover", function(){ console.log("moused over"); }); 
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div class="btn-group"> <a href="https://google.com" target="_blank" class="btn btn-xs btn-inverse"> <i class="stm stm-goog">X</i>&nbsp;&nbsp; <!-- <-- You missed a semi-colon here. --> </a> <a href="#" data-id="29508" class="btn btn-xs btn-inverse btn-modify"> <i class="fa fa-pencil"></i> Edit </a> <a href="#" data-page-modal="https://*randomwebsite*.com/manage/stats/301&amp;q=11" class="btn btn-xs btn-inverse"> <i class="fa fa-bar-chart">X</i> </a> <button type="button" class="btn btn-xs btn-danger btn-submit"><i class="fa fa-trash-o"></i>TEST</button></div> 

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

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