如何从jQuery中的一组按钮中定位一个按钮,而没有类名称或ID

发布于 2025-02-09 16:44:17 字数 528 浏览 2 评论 0原文

我如何在不使用类或ID的情况下从一组按钮中定位一个按钮,只是n jQuery

示例:

<body>
  <button>Click Me</button>
  <button>Click Me</button>
  <button class="food">Click Me</button>
  <button>Click Me</button>
  <button>Click Me</button>
  <input type="text">
  <!--
     <script src="ajax.googleapis.com/ajax/libs/jquery/3.6.0/…>
     <script src="index.js"></script>
  -->
 </body>

How can I target a button from a group of buttons without using a class or id, just n JQuery

Example:

<body>
  <button>Click Me</button>
  <button>Click Me</button>
  <button class="food">Click Me</button>
  <button>Click Me</button>
  <button>Click Me</button>
  <input type="text">
  <!--
     <script src="ajax.googleapis.com/ajax/libs/jquery/3.6.0/…>
     <script src="index.js"></script>
  -->
 </body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黯淡〆 2025-02-16 16:44:17

使用jQuery,只有在按钮具有独特的Innertext或Innerhtml时,您才有可能预测。

使用我的示例html从下面:

<div>
  <button>Button1</button>
  <button>Button2</button>
  <button>Button3</button>
  <button>Button4</button>
</div>

您需要使用jQuery抓住所有标签,然后根据按钮标签中的内容,通过其InnerHTML或Innertext过滤。

请参阅下面的JS:

// Assuming you want 'Button 1'
for(let el of $("button")){
  if(el.innerText === 'Button1') el.addEventListener() // Then from here do the rest
}

With JQuery this would only be possible if the button's had unique innerText or innerHTML that you could then predict.

Using my example HTML from below:

<div>
  <button>Button1</button>
  <button>Button2</button>
  <button>Button3</button>
  <button>Button4</button>
</div>

You would need to use JQuery to grab all tags, and then filter that by their innerHTML or innerText depending on what is inside the button tag.

See the below JS:

// Assuming you want 'Button 1'
for(let el of $("button")){
  if(el.innerText === 'Button1') el.addEventListener() // Then from here do the rest
}

咆哮 2025-02-16 16:44:17

有很多方法,您可以使用OnClick方法内联HTML。一种粗略的方式---

<script>

function function1(){

alert ('Button1')
}

function function2(){

alert ('Button2')
}
function function3 (){

alert ('Button3')
}

</script>
</head>
<body>

<button onclick="function1()">Button 1</button>

<button onclick="function2()">Button 2</button>
<button onclick="function3()">Button 3</button>
</body>

There many ways, You can use onclick method inline Html. A crude way---

<script>

function function1(){

alert ('Button1')
}

function function2(){

alert ('Button2')
}
function function3 (){

alert ('Button3')
}

</script>
</head>
<body>

<button onclick="function1()">Button 1</button>

<button onclick="function2()">Button 2</button>
<button onclick="function3()">Button 3</button>
</body>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文