簡體   English   中英

html、css 和 javascript 的新手。 我無法從 onclick 的下拉菜單中將變量傳遞給我的 javascript 函數

[英]New to html,css, and javascript. I am having trouble passing variables to my javascript function from a drop down menu onclick

我假設我犯了一個語法錯誤,但無法在谷歌上找到一個接近我的例子。 希望任何人都願意提供任何幫助。 我今天剛開始使用 HTML、CSS 和 JavaScript。 我認為這可能是我將值傳遞給函數的方式。 但是,當我查找傳遞值時, functionName(value) 似乎是正確的格式。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script>
    // Function to change webpage background color
    function changeBodyBg(color){
        document.body.style.background = color;
    }
</script>
<style>
.dropbtn {
  background-color: #4CAF50;
  color: white;
  padding: 16px;
  font-size: 16px;
  border: none;
}

.dropdown {
  position: relative;
  display: inline-block;
}

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

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

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

.dropdown:hover .dropdown-content {display: block;}

.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>

<h2>Hoverable Dropdown</h2>
<p>Move the mouse over the button to open the dropdown menu.</p>

<div class="dropdown">
  <button class="dropbtn">Dropdown</button>
  <div class="dropdown-content">
    <a href="#" onclick="changeBodyBg(red)">Red</a>
    <a href="#" onclick="changeBodyBg(blue)">Blue</a>
    <a href="#" onclick="changeBodyBg(green)">Green</a>
  </div>
</div>

</body>
</html>

您只需要在字符串函數參數周圍加上單引號'' 當您只使用顏色名稱時,您的代碼正在尋找名為red / green / blue的變量,而不是將其解釋為字符串。

<a href="#" onclick="changeBodyBg('red')">Red</a>
<a href="#" onclick="changeBodyBg('blue')">Blue</a>
<a href="#" onclick="changeBodyBg('green')">Green</a>

 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <script> // Function to change webpage background color function changeBodyBg(color){ document.body.style.background = color; } </script> <style> .dropbtn { background-color: #4CAF50; color: white; padding: 16px; font-size: 16px; border: none; } .dropdown { position: relative; display: inline-block; } .dropdown-content { display: none; position: absolute; background-color: #f1f1f1; min-width: 160px; box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2); z-index: 1; } .dropdown-content a { color: black; padding: 12px 16px; text-decoration: none; display: block; } .dropdown-content a:hover {background-color: #ddd;} .dropdown:hover .dropdown-content {display: block;} .dropdown:hover .dropbtn {background-color: #3e8e41;} </style> </head> <body> <h2>Hoverable Dropdown</h2> <p>Move the mouse over the button to open the dropdown menu.</p> <div class="dropdown"> <button class="dropbtn">Dropdown</button> <div class="dropdown-content"> <a href="#" onclick="changeBodyBg('red')">Red</a> <a href="#" onclick="changeBodyBg('blue')">Blue</a> <a href="#" onclick="changeBodyBg('green')">Green</a> </div> </div> </body> </html>

當您將字符串作為參數傳遞給函數時,它需要放在引號中。 如果您的函數已經在雙引號中,您可以使用單引號來傳遞字符串。 像這樣 -

onclick="changeColor('myColor')"

所以正確的語法是

<div class="dropdown">
 <button class="dropbtn">Dropdown</button>
   <div class="dropdown-content">
     <a href="#" onclick="changeBodyBg('red')">Red</a>
     <a href="#" onclick="changeBodyBg('blue')">Blue</a>
     <a href="#" onclick="changeBodyBg('green')">Green</a>
   </div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM