【问题标题】:(Javascript) How do I link sub menu buttons to a function?(Javascript)如何将子菜单按钮链接到功能?
【发布时间】:2019-03-09 07:17:31
【问题描述】:

我即将完成一个项目,但我一直在尝试将按钮链接到我的功能。我成功编辑了菜单,但我需要按钮来链接到我创建的函数。

例如当我看

<a href="#">Add a side to your Dice</a>
<a href="#">Remove a side to your Dice</a>
<a href="#">Add an additional Roll</a>

我将如何添加这些函数以便它们实际执行某些操作?

onClick="promoteDice()
onClick="demoteDice()
onClick="addRoll()

标签也一样:

  <a href="#roll">Roll</a>
  <a href="#exit">Exit</a>

我没有看到我应该在哪里添加该功能以使其正常工作。

这是我的实际骰子游戏供参考我不需要你合并我的脚本我只需要帮助解决这个问题。

<!DOCTYPE html>
<html>
    <body>
    <input type="button" value="Roll The Dice" onClick="rollDice()" />
    <input type="button" value="Upgrade The Dice" onClick="upgradeDice()" />
    <input type="button" value="Quit" onClick="quit()" />
<script>    
var myscore = 0;
var housescore = 0;
var dicesides = 6;
var diceroll = 1

function rollDice() {
  var x = Math.floor(Math.random() * dicesides) + 1;
  var y = Math.floor(Math.random() * dicesides) + 1;

  var total = x + y;
  msg = "You rolled " + x + ", " + y + " = " + total + "\n";
    if (total != 11 && total < 7 && x != y) {
            housescore += 1;
      msg += "Even Up";
        } else if (total != 11 && total > 7 && x != y) {
            myscore += 1;
      msg += "Player Up";
        }
  if (total == 7 || total == 11) {
    housescore += 10;
    msg += "CRAPS";
  } else if (x == y) {
    if (x % 2 == 0) {
      myscore += 10;
      msg += "Even Up";
    } else {
      housescore += 10;
      msg += "Odd Ball";
    }
  }
    msg += " Your Score is " + "Player: " + myscore + ", House: " + housescore;
  alert(msg);
    didIwin();

}
function didIwin() {
    if (housescore >= 100) {
    alert ("Sorry the house won the game!");
    } else if (playerscore >= 100) {
    alert ("Congratulations you won the game!");
    }
    myscore = 0;
    housescore = 0;
    dicesides = 6;
}
function upgradeDice() {
    if (myscore >= 10) {
    dicesides += 1;
    alert ("Your Dice now have " + dicesides + " sides");
    } else {
difference = 10 - myscore;
alert ("You need "+difference+" more points");
    }
}
function quit() {
  alert (" Your Score is " + "Player: " + myscore + ", House: " + housescore);
    myscore = 0;
    housescore = 0;
    dicesides = 6;
    }




</script>
</body>
</html>

最后这里是我想要合并功能的菜单代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  cursor: pointer;
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.show {
  display: block;
}
</style>
</head>
<body>

<div class="navbar">
  <a href="#roll">Roll</a>
  <a href="#exit">Exit</a>
  <div class="dropdown">
  <button class="dropbtn" onclick="myFunction()">Upgrade
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-content" id="myDropdown">
    <a href="#">Add a side to your Dice</a>
    <a href="#">Remove a side to your Dice</a>
    <a href="#">Add an additional Roll</a>
  </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Click on the "Dropdown" link to see the dropdown menu.</p>

<script>
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {
  var myDropdown = document.getElementById("myDropdown");
    if (myDropdown.classList.contains('show')) {
      myDropdown.classList.remove('show');
    }
  }
}
</script>
</body>
</html>

【问题讨论】:

  • 您必须将您尝试调用的那些内联函数(例如 onclick="promoteDice()")添加到您的 JS 中。例如function promoteDice() { /* code here */ }
  • 我不知道这是否重要,但onclick 通常是小写的。见:stackoverflow.com/questions/4380719/onclick-or-onclick
  • 我完成了掷骰子和退出的编码,如何将它们附加到按钮上?我的脚本工作正常,但要完成,我需要升级按钮有一个子菜单,一旦我学会如何做,我将完成编码

标签: javascript button nested submenu


【解决方案1】:

您可以像在之前的示例中那样添加功能,您是否在子菜单中或其他什么地方并不重要。

只需将onclick 属性和函数添加到要触发执行的 HTML 标记中,然后就可以开始了。

在我的示例中,我将函数 myFunc(); 添加到 dice 子菜单的添加侧,但您可以将任何逻辑和任何其他内容添加到其余 HTML 标记中,这只是一个示例。

附带说明:函数myFunc(); 内的代码可以更改为任何内容。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.navbar {
  overflow: hidden;
  background-color: #333;
  font-family: Arial, Helvetica, sans-serif;
}

.navbar a {
  float: left;
  font-size: 16px;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.dropdown {
  float: left;
  overflow: hidden;
}

.dropdown .dropbtn {
  cursor: pointer;
  font-size: 16px;  
  border: none;
  outline: none;
  color: white;
  padding: 14px 16px;
  background-color: inherit;
  font-family: inherit;
  margin: 0;
}

.navbar a:hover, .dropdown:hover .dropbtn, .dropbtn:focus {
  background-color: red;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: #f9f9f9;
  min-width: 160px;
  box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
  z-index: 1;
}

.dropdown-content a {
  float: none;
  color: black;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
  text-align: left;
}

.dropdown-content a:hover {
  background-color: #ddd;
}

.show {
  display: block;
}
</style>
</head>
<body>

<div class="navbar">
  <a href="#roll">Roll</a>
  <a href="#exit">Exit</a>
  <div class="dropdown">
  <button class="dropbtn" onclick="myFunction()">Upgrade
    <i class="fa fa-caret-down"></i>
  </button>
  <div class="dropdown-content" id="myDropdown">
    <a href="#" onclick="myFunc();">Add a side to your Dice</a>
    <a href="#">Remove a side to your Dice</a>
    <a href="#">Add an additional Roll</a>
  </div>
  </div> 
</div>

<h3>Dropdown Menu inside a Navigation Bar</h3>
<p>Click on the "Dropdown" link to see the dropdown menu.</p>

<script>

function myFunc() {
  alert('You clicked a submenu item')
}
/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(e) {
  if (!e.target.matches('.dropbtn')) {
  var myDropdown = document.getElementById("myDropdown");
    if (myDropdown.classList.contains('show')) {
      myDropdown.classList.remove('show');
    }
  }
}
</script>
</body>
</html>

【讨论】:

    【解决方案2】:

    您只需像这样将 onclick 添加到链接中:

    <a href="#" onclick="promoteDice()">Add a side to your Dice</a>
    <a href="#" onclick="demoteDice()">Remove a side to your Dice</a>
    <a href="#" onclick="addRoll()">Add an additional Roll</a>
    

    您可能还想改用&lt;button type="button" onclick="addRoll()"&gt;Add Roll&lt;/button&gt;,因为&lt;a&gt; 标签只应在您链接到某物时使用。

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2012-12-11
      • 1970-01-01
      • 2017-07-04
      • 2011-07-19
      相关资源
      最近更新 更多