为什么我的JavaScript/JQuery添加和删除带有函数的类不起作用?
嗨,伙计们,我今天遇到了大问题 当用户在字段中键入文本,然后按submit时,用户输入的文本应该出现在图像下方的标题上,该标题以前是为什么我的JavaScript/JQuery添加和删除带有函数的类不起作用?,javascript,html,jquery,css,bootstrap-4,Javascript,Html,Jquery,Css,Bootstrap 4,嗨,伙计们,我今天遇到了大问题 当用户在字段中键入文本,然后按submit时,用户输入的文本应该出现在图像下方的标题上,该标题以前是。不可见的,但我在用户按submit时使用jquery将其删除。我还使用.html将html更改为用户的文本输入。我已经标出了这一部分(请参见注释://显示用户在javascript中选择的标题) 然而,这并没有发生…有人能帮忙吗谢谢 以下是我的javascript: function diceRoll() { // 1st dice var ran
。不可见的
,但我在用户按submit时使用jquery将其删除。我还使用.html
将html更改为用户的文本输入。我已经标出了这一部分(请参见注释://显示用户在javascript中选择的标题)
然而,这并没有发生…有人能帮忙吗谢谢
以下是我的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 = []
从改变这个开始
var noOfChoices = $("#dropdownMenu").text();
到
要从下拉框中获取文本,请尝试更改此选项
var noOfChoices = $("#dropdownMenu").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();
});
//响应下拉按钮
let links=document.queryselectoral(“#list li”);
links.forEach((el)=>{
el.addEventListener(“单击”,(事件)=>{
让val=event.target.innerText;
document.getElementById(
“下拉菜单”
).innerHTML=`${val}`;
如果(val==“2”){/*numberOfChoices更改为val*/
$(“#img3,#img4,.threeChoices,.fourChoices”).addClass(“不可见”);
}
如果(val==“3”){/*numberOfChoices更改为val*/
$(“#img4,.fourChoices”).addClass(“不可见”);
$(“#img3,.ThreeChooses”).removeClass(“不可见”);
}
如果(val==“4”){/*numberOfChoices更改为val*/
$(“#img3,#img4,.threeChoices,.fourChoices”).removeClass(“不可见”);
}
});
});
//答复提交
document.getElementById(“提交”).addEventListener(“单击”),函数(e){
e、 预防默认值();
//将数据存储到变量中
var choice1=$(“#choice1”).val();
var choice2=$(“#choice2”).val();
var choice3=$(“#choice3”).val();
var choice4=$(“#choice4”).val();
var noOfChoices=$(“#下拉菜单”).text().trim();/*在此处添加.trim()*/
//旋转动画
$(“.dice”).addClass(“旋转”);
设置超时(()=>{
$(“.dice”).removeClass(“旋转”);
}, 1000);
//将文本更改为用户输入
$(“#字幕1”).html(选项1);
$(“#字幕2”).html(选项2);
$(“#字幕3”).html(选项3);
$(“#字幕4”).html(选项4);
//显示用户选择的标题数
如果(无选择==“2”){
$(“#字幕1,#字幕2”).removeClass(“不可见”);
$(“#字幕3,#字幕4”).addClass(“不可见”);
}
如果(无选择==“3”){
$(“#字幕1,#字幕2,#字幕3”)。删除类(“不可见”);
$(“#字幕4”).addClass(“不可见”);
}
如果(无选择==“4”){
$(“.caption”).removeClass(“不可见”);
}
//掷骰子
骰子卷();
});
我运行了您的代码,似乎将var noOfChoices=$(“#下拉菜单”).text()更改为var noOfChoices=$(“#下拉菜单”).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();
});
//响应下拉按钮
let links=document.queryselectoral(“#list li”);
links.forEach((el)=>{
el.addEventListener(“单击”,(事件)=>{
让val=event.target.innerText;
document.getElementById(
“下拉菜单”
).innerHTML=`${val}`;
如果(val==“2”){/*numberOfChoices更改为val*/
$(“#img3,#img4,.threeChoices,.fourChoices”).addClass(“不可见”);
}
如果(val==“3”){/*numberOfChoices更改为val*/
$(“#img4,.fourChoices”).addClass(“不可见”);
$(“#img3,.ThreeChooses”).removeClass(“不可见”);
}
如果(val==“4”){/*numberOfChoices更改为val*/
$(“#img3,#img4,.threeChoices,.fourChoices”).removeClass(“不可见”);
}
});
});
//答复提交
document.getElementById(“提交”).addEventListener(“单击”),函数(e){
e、 预防默认值();
//将数据存储到变量中
var choice1=$(“#choice1”).val();
var choice2=$(“#choice2”).val();
var choice3=$(“#choice3”).val();
var choice4=$(“#choice4”).val();
var noOfChoices=$(“#下拉菜单”).text().trim();/*在此处添加.trim()*/
//旋转动画
$(“.dice”).addClass(“旋转”);
设置超时(()=>{
$(“.dice”).removeClass(“旋转”);
}, 1000);
//将文本更改为用户输入
$(“#字幕1”).html(选项1);
$(“#字幕2”).html(选项2);
$(“#字幕3”).html(选项3);
$(“#字幕4”).html(选项4);
//显示用户选择的标题数
如果(无选择==“2”){
$(“#字幕1,#字幕2”).removeClass(“不可见”);
$(“#字幕3,#字幕4”).addClass(“不可见”);
}
如果(无选择==“3”){
$(“#字幕1,#字幕2,#字幕3”)。删除类(“不可见”);
$(“#字幕4”).addClass(“不可见”);
}
如果(无选择==“4”){
$(“.caption”).removeClass(“不可见”);
}
//掷骰子
骰子卷();
});
但我能够使用var noOfChoices=$(“#dropdownlemon”).text()记录noOfChoices代码>之前。我不知道出了什么问题,如果您使用Chrome,请尝试检查控制台。由于您正在使用的id不存在,您将在某些getElementById方面遇到一些错误。-document.getElementById('dropdownMenu1')。innerHTML=${numberOfChoices}
但是我能够使用var noOfChoices=$(“#dropdownMenu”).text()记录noOfChoices代码>之前。我不知道出了什么问题
// 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();
});