简体   繁体   English

如何添加2个onclick函数

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

How do I add a string of text before each onclick event. 如何在每个onclick事件之前添加文本字符串。 For example the output for the second button is "6" while i want it to be "the number of items in the list is: 6" 例如,第二个按钮的输出为“ 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>

Hello put the text between 'text' and after add + . 您好将文本放在'text'和add +之后。
like this: 像这样:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM