[英]Why is my JavaScript/JQuery adding and removing class with function not working?
大家好,我今天遇到了重大問題。
當用戶在字段上鍵入文本然后按提交時,用戶輸入的文本應出現在圖像下方的標題上,以前是.invisible
但我在用戶按提交時使用 jquery 將其刪除。 我還使用.html
將 html 更改為用戶的文本輸入。 我已經標記了這部分(請參閱評論: // Displaying captions that user chose
在 javascript 中// Displaying captions that user chose
)。
然而這並沒有發生......有人可以幫忙嗎謝謝......
這是我的javascript:
function diceRoll() {
// 1st dice
var randomNumber1 = Math.floor(Math.random() * 6 + 1);
var Image1 = "dice" + randomNumber1 + ".png";
document.querySelectorAll("img")[1].setAttribute("src", Image1);
// 2nd dice
var randomNumber2 = Math.floor(Math.random() * 6 + 1);
var Image2 = "dice" + randomNumber2 + ".png";
document.querySelectorAll("img")[2].setAttribute("src", Image2);
// 3rd dice
var randomNumber3 = Math.floor(Math.random() * 6 + 1);
var Image3 = "dice" + randomNumber3 + ".png";
document.querySelectorAll("img")[3].setAttribute("src", Image3);
// 4th dice
var randomNumber3 = Math.floor(Math.random() * 6 + 1);
var Image4 = "dice" + randomNumber3 + ".png";
document.querySelectorAll("img")[4].setAttribute("src", Image4);
}
// Responding to Dropdown Button
let links = document.querySelectorAll('#list li')
links.forEach((el) => {
el.addEventListener('click', (event) => {
let numberOfChoices = event.target.innerText
document.getElementById('dropdownMenu').innerHTML = `${numberOfChoices}<span class="caret"></span>`
if (numberOfChoices === "2") {
$("#img3, #img4, .threeChoices, .fourChoices").addClass("invisible")
}
if (numberOfChoices === "3") {
$("#img4, .fourChoices").addClass("invisible");
$("#img3, .threeChoices").removeClass("invisible")
}
if (numberOfChoices === "4") {
$("#img3, #img4, .threeChoices, .fourChoices").removeClass("invisible");
}
})
})
// Responding to Submit
document.getElementById("submit").addEventListener("click", function(e) {
e.preventDefault();
// Storing Data into variables
var choice1 = $("#choice1").val();
var choice2 = $("#choice2").val();
var choice3 = $("#choice3").val();
var choice4 = $("#choice4").val();
var noOfChoices = $("#dropdownMenu").text();
// Rotate animation
$(".dice").addClass("rotate");
// Changing text to user input
$("#caption1").html(choice1);
$("#caption2").html(choice2);
$("#caption3").html(choice3);
$("#caption4").html(choice4);
// Displaying captions that user chose
if (noOfChoices === "2") {
$("#caption1, #caption2").removeClass("invisible");
$("#caption3, #caption4").addClass("invisible");
}
if (noOfChoices === "3") {
$("#caption1, #caption2, #caption3").removeClass("invisible");
$("#caption4").addClass("invisible");
}
if (noOfChoices === "4") {
$(".caption").removeClass("invisible");
}
// Rolling Dice
diceRoll();
// Determining winner
})
var diceNumbers = []
這是我的 html:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Diceey</title>
<!-- Bootstrap, CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="styles.css">
<!-- Jquery Links -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
<!-- Images -->
<div class="container-fluid">
<div class="container-of-images">
<img src="chick2.png">
<figure>
<img id="img1" class="dice" src="dice6.png">
<figcaption class="caption" id="caption1">Choice 1</figcaption>
</figure>
<figure>
<img id="img2" class="dice" src="dice6.png">
<figcaption class="caption" id="caption2">Choice 2</figcaption>
</figure>
<figure class="threeChoices">
<img id="img3" class="dice" src="dice6.png">
<figcaption class="caption" id="caption3">Choice 3</figcaption>
</figure>
<figure class="fourChoices">
<img id="img4" class="dice" src="dice6.png">
<figcaption class="caption" id="caption4">Choice 4</figcaption>
</figure>
<img src="chick1.png">
</div>
</div>
<!-- Forms -->
<div class="container-fluid">
<h5>Write down your choices here (up to 4):</h5>
<div class="container-of-forms">
<!-- Dropdown Button -->
<div class="dropdown">
<button class="btn btn-info dropdown-toggle" type="button" id="dropdownMenu" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
2
<span class="caret"></span>
</button>
<ul id="list" class="dropdown-menu dropdown-info" aria-labelledby="dropdownMenu">
<li><a href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
</ul>
<!-- Input Text Fields -->
<div class="container-inner">
<input class="text-input-box" id="choice1" type="text" name="" value="" placeholder="Choice 1"> <br>
<input class="text-input-box" id="choice2" type="text" name="" value="" placeholder="Choice 2"> <br>
<input class="text-input-box invisible threeChoices" id="choice3" type="text" name="" value="" placeholder="Choice 3"> <br>
<input class="text-input-box invisible fourChoices" id="choice4" type="text" name="" value="" placeholder="Choice 4">
</div>
</div>
</div>
<!-- Submit Button -->
<a href="" id="submit" class="btn btn-info btn-lg" role="button">Go</a>
</div>
<script src="index.js" charset="utf-8">
</script>
</body>
</html>
這是我的 css:
img {
width: 80px;
line-height: 0;
margin: 0 1%;
display: inline-block;
justify-content: center;
vertical-align: middle;
}
figcaption {
text-align:
center;
width: 100px;
font-size: 1.5rem;
color: #5b8c85;
font-family: 'Indie Flower', cursive;
}
figure {
display: inline-block;
vertical-align: middle;
}
.rotate {
animation: rotation 0.3s infinite linear;
animation-iteration-count: 2;
}
@keyframes rotation {
from {
transform: rotate(0deg);
}
to {
transform: rotate(359deg);
}
}
.invisible {
display: none;
}
.hidden {
visibility: hidden;
}
從改變這個開始
var noOfChoices = $("#dropdownMenu").text();
到
var noOfChoices = $("#dropdownMenu option:selected").text();
要從下拉框中獲取文本。嘗試
我運行了您的代碼,似乎將var noOfChoices = $("#dropdownMenu").text()
更改為var noOfChoices = $("#dropdownMenu").text().trim()
可以解決問題。
以某種方式獲取下拉菜單 text() 將在開頭有空格,這就是為什么所有 if 語句總是返回 false 的原因。
這應該是帶有一些其他修改的 js 代碼,您會發現這些修改已被注釋。
// Responding to Dropdown Button
let links = document.querySelectorAll("#list li");
links.forEach((el) => {
el.addEventListener("click", (event) => {
let val = event.target.innerText;
document.getElementById(
"dropdownMenu"
).innerHTML = `${val}<span class="caret"></span>`;
if (val === "2") { /* numberOfChoices changed to val */
$("#img3, #img4, .threeChoices, .fourChoices").addClass("invisible");
}
if (val === "3") { /* numberOfChoices changed to val */
$("#img4, .fourChoices").addClass("invisible");
$("#img3, .threeChoices").removeClass("invisible");
}
if (val === "4") { /* numberOfChoices changed to val */
$("#img3, #img4, .threeChoices, .fourChoices").removeClass("invisible");
}
});
});
// Responding to Submit
document.getElementById("submit").addEventListener("click", function (e) {
e.preventDefault();
// Storing Data into variables
var choice1 = $("#choice1").val();
var choice2 = $("#choice2").val();
var choice3 = $("#choice3").val();
var choice4 = $("#choice4").val();
var noOfChoices = $("#dropdownMenu").text().trim(); /* adding .trim() here */
// Rotate animation
$(".dice").addClass("rotate");
setTimeout(() => {
$(".dice").removeClass("rotate");
}, 1000);
// Changing text to user input
$("#caption1").html(choice1);
$("#caption2").html(choice2);
$("#caption3").html(choice3);
$("#caption4").html(choice4);
// Displaying no. of Captions that User Chose
if (noOfChoices === "2") {
$("#caption1, #caption2").removeClass("invisible");
$("#caption3, #caption4").addClass("invisible");
}
if (noOfChoices === "3") {
$("#caption1, #caption2, #caption3").removeClass("invisible");
$("#caption4").addClass("invisible");
}
if (noOfChoices === "4") {
$(".caption").removeClass("invisible");
}
// Rolling Dice
diceRoll();
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.