[英]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.