如何从querySelectorAll中获取按钮类型

How to fetch button type from querySelectorAll

本文关键字:获取 按钮类型 querySelectorAll      更新时间:2023-09-26

我是这个问题的新手。我想在querySelectorAll 中选择以下按钮类型

<button type="submit">Comment</button>

我还可以检查type=submit是否包含"Comment"?

感谢

希望这有帮助:

document.querySelectorAll("button[type=submit]");

使用button[type=submit]将对您有所帮助。你也可以访问它的风格和价值。

检查:

function myFunction() {
   
var y = document.querySelectorAll("button[type=submit]");
y[0].style.color = "blue";
  
  
var txt = y[0].textContent || y[0].innerText;
alert(txt);
  
  
}
<button type="submit">Comment</button>
<button onclick="myFunction()">Try it</button>

尝试向按钮添加一个类并执行以下操作:

var matches = document.querySelectorAll("button.comment");
console.log(matches);
<button type="submit" class="comment">Comment</button>
<button type="submit" class="comment">Comment</button>

这将返回一个具有comment类的按钮列表。

https://jsfiddle.net/nso2bzm6/