功能只有在 Vue 中编辑和刷新后才能工作?
我在 Vue 中的功能似乎只有在故意引发错误然后刷新页面后才起作用。该代码工作正常,只是初始化不起作用。 有人可以帮忙吗?
简要说明:
当用户点击按钮时,会调用名为 provjeriOdgovore
的函数。 该函数查找类名 answer
的所有元素(输入字段),然后进行迭代,检查输入值是否包含在变量 odgovori
内。如果是,程序会将 green-color
类附加到 answer
的 classList 中,如果不是,则会发生相同的情况,但不是 green- color
,它是 red-color
有问题的代码:
HTML 部分:
<input class="answer" type="text">
<input class="answer" type="text">
<input class="answer" type="text">
<button :onclick="provjeriOdgovore()">Provjeri</button>
Javascript 部分:
const odgovori = [[long array of strings], [long array of strings], [long array of strings]]
export default {
data() {
return {
provjeriOdgovore: () => {
let questionDiv = document.getElementsByClassName("answer")
for (let i = 0; i < questionDiv.length; i++) {
let userInput = questionDiv[i].value.toLowerCase();
questionDiv[i].classList.add("color-white")
if (odgovori[i].includes(userInput)) {
console.log("tocno", questionDiv[i])
questionDiv[i].classList.add("green-color");
if (questionDiv[i].classList.length > 2) {
questionDiv[i].classList.remove("red-color");
}
} else {
console.log("netocno" ,questionDiv[i])
questionDiv[i].classList.add("red-color");
if (questionDiv[i].classList.length > 2) {
questionDiv[i].classList.remove("green-color");
}
}
}
}
}
}
}
My function in Vue seems to only work after intentionally causing an error and then refreshing the page. The code works fine it's just the initialization that's isn't working.
Can anyone help?
Brief explanation:
When the user hits the button, a function is called by the name of provjeriOdgovore
.
The function looks for all the elements with the class name answer
(the input fields), and then iterates, checking if the value of the input is contained inside of the variable odgovori
. If it is, the program appends the class green-color
to the classList of answer
, if it isn't, the same thing happens but instead of green-color
, it's red-color
Code in question:
HTML part:
<input class="answer" type="text">
<input class="answer" type="text">
<input class="answer" type="text">
<button :onclick="provjeriOdgovore()">Provjeri</button>
Javascript part:
const odgovori = [[long array of strings], [long array of strings], [long array of strings]]
export default {
data() {
return {
provjeriOdgovore: () => {
let questionDiv = document.getElementsByClassName("answer")
for (let i = 0; i < questionDiv.length; i++) {
let userInput = questionDiv[i].value.toLowerCase();
questionDiv[i].classList.add("color-white")
if (odgovori[i].includes(userInput)) {
console.log("tocno", questionDiv[i])
questionDiv[i].classList.add("green-color");
if (questionDiv[i].classList.length > 2) {
questionDiv[i].classList.remove("red-color");
}
} else {
console.log("netocno" ,questionDiv[i])
questionDiv[i].classList.add("red-color");
if (questionDiv[i].classList.length > 2) {
questionDiv[i].classList.remove("green-color");
}
}
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要放入您的方法,
然后调用它...如果您希望它在渲染或创建时独立运行,请在任何生命周期钩子中调用它
You need to put your method in,
and then call it... if you want it run on it's own while rendering or creation call it in any lifecyclehook