3 回答
TA贡献1765条经验 获得超5个赞
<html>
<head>
<script>
function tagName(tag) {
var buttons = document.getElementsByTagName(tag);
var totalButton = buttons.length;
var r = []
for (var i = 0; i < totalButton; i++) {
// here i want to check which button clicked?
r.push(buttons[i])
}
return r;
};
function onLoad() {
tagName("button").forEach(function (item, idx) {
item.addEventListener('click', function () {
document.getElementById("result").innerHTML = item.innerHTML;
});
});
}
</script>
</head>
<body onload="onLoad()">
<button>Button 1</button>
<button>Button 2</button>
<p id="result"></p>
<script type="text/javascript">
</script>
</body>
</html>
TA贡献1812条经验 获得超5个赞
尝试这样的事情:
var obj = document.getElementsByTagName("button");
for (let item of obj) {
item.onclick = function(){
document.getElementById("result").innerHTML = item.innerHTML;
}
}
添加回答
举报