功能只有在 Vue 中编辑和刷新后才能工作?

发布于 2025-01-09 19:48:43 字数 1756 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-01-16 19:48:43

您需要放入您的方法,

methods: {
    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");
                }
            }
        }
    },
},

然后调用它...如果您希望它在渲染或创建时独立运行,请在任何生命周期钩子中调用它

You need to put your method in,

methods: {
    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");
                }
            }
        }
    },
},

and then call it... if you want it run on it's own while rendering or creation call it in any lifecyclehook

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文