简体   繁体   English

如何在javascript中单击按钮的内部文本?

[英]How do you click a button by its inner text in javascript?

I have the following piece of html code: 我有以下一段html代码:

<div class ="listit">
  <a href="javascript:dB()">Update</a>
  <a href="post.php?f=54&y=5">New iso</a>
  <a href="post.php?f=54&y=2">New check></a>
</div>

The buttons look like that: 这些按钮如下所示: 在此处输入图片说明

Now I like to press the button called Update by using its name (but I'm also very interested to see if it works without using its name). 现在,我想通过使用其名称来按下名为“ 更新”的按钮(但是我也很想知道它是否在不使用其名称的情况下起作用)。

I have already tried to go through the class and collect the names using loop: 我已经尝试遍历该类并使用循环收集名称:

var x = document.getElementsByClassName("listit");
for (var i = 0; i < x.length; i++) {
  var counter = x[i].innerText;
  if(counter == "Update"){

  }
}

And I think I almost have it, I just could not find a working way to use that click() function so I click the button Update by name? 而且我想我差不多了,我只是找不到使用该click()函数的有效方法,所以我单击了按名称更新按钮? There is not something like document.getElementByInnerText() ? 没有类似于document.getElementByInnerText()东西吗? I don't know how to get this work..? 我不知道如何进行这项工作。 :S :S

You are selecting the surrounding div (which actually has the CSS class listit ), not the links inside it. 您正在选择周围的div (实际上具有CSS类listit ),而不是其中的链接。

 var x = document.getElementsByClassName("listit"); console.log(x); 
 <div class ="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check&gt;</a> </div> 

If you need a collection of the links, use document.querySelectorAll('.listit a') : 如果需要链接的集合,请使用document.querySelectorAll('.listit a')

 function dB() { console.log('dB called'); } const links = Array.from(document.querySelectorAll('.listit a')); links.forEach((link) => { if (link.textContent === 'Update') { link.click(); } }) 
 <div class ="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check&gt;</a> </div> 

Sidenote : There are certain characters that need to be replaced by their so-called HTML entities to be usable inside HTML tag content, the most important being these: 旁注 :必须将某些字符替换为所谓的HTML实体,才能在HTML标签内容中使用,其中最重要的是:

&<>

So I replaced your 所以我换了你

<a href="post.php?f=54&y=2">New check></a> 

by 通过

<a href="post.php?f=54&y=2">New check&gt;</a>

You need to loop over the child elements too to get the button since your class selector works on the parent div. 您还需要遍历子元素以获取按钮,因为您的类选择器适用于父div。

This is really bad design though. 但是,这确实是一个糟糕的设计。 If you have the ability to change the html please do so. 如果您有能力更改html,请这样做。

 var mainElement = document.getElementsByClassName("listit"); for (var i = 0; i < mainElement.length; i++) { var children = mainElement[i].childNodes; for (var j = 0; j < children.length; j++) { if (children[j].innerText == "Update") { console.log('found by innertext'); console.log(children[j]); } } } 
 <div class="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check></a> </div> 

You can even get the element by the href attribute if that's an options for you. 如果您可以选择的话,甚至可以通过href属性获取元素。 Al tough this looks very dodgy too. Al Hard这看起来也很狡猾。

 var element = document.querySelector('.listit a[href="javascript:dB()'); console.log(element); 
 <div class="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check></a> </div> 

This is how the button works. 这就是按钮的工作方式。 By putting javascript: in the href the button executes the part of the href after the : . 通过将javascript:在href按钮执行的部分href: So in this case dB() which means execution of the function named db . 因此,在这种情况下, dB()意味着执行名为db的函数。

 function dB() { console.log('update button pressed'); } 
 <div class="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check></a> </div> 

You would create a mouseEvent and use dispatchEvent to fire it. 您将创建一个mouseEvent并使用dispatchEvent触发它。 (You also need to loop over listit 's children, rather than the div itself): (您还需要遍历listit的孩子,而不是div本身):

 var dB = function() {console.log("CLICKED")} var x = document.getElementsByClassName("listit")[0].children; for (var i = 0; i < x.length; i++) { var counter = x[i].innerText; if (counter == "Update") { x[i].dispatchEvent( new MouseEvent('click') ) } } 
 <div class="listit"> <a href="javascript:dB()">Update</a> <a href="post.php?f=54&y=5">New iso</a> <a href="post.php?f=54&y=2">New check></a> </div> 

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

相关问题 如何根据按钮的内部文本单击按钮? VB.Net - How to click on a button based on its inner text? VB.Net 如何根据其文本 JavaScript 单击按钮? - How to click the button according to its text JavaScript? 你如何使用 javascript 获取这些类中的内部文本? - How do you get the inner text inside these classes with javascript? 您如何使用javascript查找并单击网页上的特定按钮? - How do you find and click on a specific button on a webpage using javascript? 如何使用python硒单击javascript按钮 - How do you click a javascript button using python selenium 您如何使用 HTML 按钮单击来调用 javascript function? - How do you call a javascript function with a HTML button click? 如何增加按钮上的点击计数器并使按钮执行其原始操作? - How do you increment a click counter on a button and make the button perform its original action? 如何在单击按钮的任意位置将特殊字符插入文本输入中? - How do you insert a special character into a text input at an arbitrary position on button click? 如何清除 Javascript 中的列表,即一键删除列表中的所有项目? - How do you clear a list in Javascript, i.e. delete all items in a list with one button click? 在javascript原型中,如何动态单击刚刚被单击的按钮? - In javascript, prototype, how do you dynamically click a button that had just been clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM