如何在不使用類或 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>
uj5u.com熱心網友回復:
使用 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
}
uj5u.com熱心網友回復:
有很多方法,你可以使用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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/494773.html
標籤:javascript html jQuery 按钮