[英]How to add 2 onclick functions
<!DOCTYPE html>
<html>
<body>
<h1>A452 Question 2 - Arrays</h1>
<p>Array of Products:</p>
<p id="prod"></p>
<p id="prod2"></p>
<p id="prod3"></p>
<p id="text1"></p>
<p id="text2"></p>
<script>
var products = ["Printer","Tablet","Router","Keyboard","Monitor","Mouse"];
document.getElementById("prod").innerHTML = products
</script>
<button type = "button" onclick = "document.getElementById('prod2').innerHTML = products.sort();">
Turn list in alphabetical order!</button>
<button type = "button" onclick = "document.getElementById('prod3').innerHTML = products.length;">
Count the number of items in the list!</button>
</body>
</html>
如何在每个onclick事件之前添加文本字符串。 例如,第二个按钮的输出为“ 6”,而我希望它为“列表中的项目数为:6”
您可以只连接字符串:
<button type = "button" onclick = "document.getElementById('prod3').innerHTML = 'the number of items in the list is: ' + products.length;">Count the number of items in the list!</button>
您好将文本放在'text'和add +之后。
像这样:
<button type = "button" onclick = "document.getElementById('prod3').innerHTML = 'the number of items in the list is: ' + products.length;">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.