2 回答
TA贡献1815条经验 获得超13个赞
var i = 0;
var txt = '"iPhone 12 Pro Max"';
var speed = 50;
var elements = document.querySelectorAll('button')
for (let i = 0; i < elements.length; i++) {
elements[i].addEventListener('click', function() {
var txt = this.getAttribute('data-product-name');
console.log(txt);
});
}
<button data-product-name="iPhone 12 Pro">iPhone 12 Pro Max</button>
<button data-product-name="Pixel 5">Pixel</button>
<button data-product-name="LG">LG</button>
<button data-product-name="Samsung S20 Plus">Samsung S20 Plus</button>
TA贡献1813条经验 获得超2个赞
您可以将按钮的 id 传递给 searchText。并使用 jquery 或纯 js 读取文本
<!--html --->
<button onclick="searchText('buttonOne')" id= "buttonOne" data-product-name="Samsung S20 Plus">Samsung S20 Plus</button><---- Added for what Id like
// javascript
function(buttonId){
var text = document.getElementById(buttonId).innerHTML
// process the text with your business logic here
}
添加回答
举报