我想知道是否可以从计算函数中通过 Vue.js 中的 id 获取元素。这是一个愚蠢的问题,但由于某种原因,当我尝试记录此条件时,它给了我 null 作为响应。
假设这些是 html 标签:
<button id="dice1" class="revol"></button>
<button id="dice2" class="revol"></button>
Run Code Online (Sandbox Code Playgroud)
然后在我的一种计算方法中,我尝试访问两个 ID
computed: {
roll(){
document.getElementById("dice1").className += "dic1";
document.getElementById("dice2").className += "dic2";
...some code
}
}
Run Code Online (Sandbox Code Playgroud)
由于错误,我检查了创建的钩子发生了什么,并意识到document.getElementById
任何 id 的返回 null
created() {
console.log(document.getElementById("dice1"));
console.log(document.getElementById("dice1"));
},
Run Code Online (Sandbox Code Playgroud)
另外,我没有直接引用 DOM 元素,而是初始化变量并将 elementsById 分配给它们,但结果是相同的
rollDice() {
var diceFirst= document.getElementById("dice1");
var diceSecond= document.getElementById("dice2")
diceFirst.className += "dic1";
diceSecond.className += "dic2";
....some code
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能改善这种情况?提前致谢!
我试图证明基于以下定义的引理。
Section lemma.
Variable A : Type.
Variable P : A -> Prop.
Variable P_dec : forall x, {P x}+{~P x}.
Inductive vector : nat -> Type :=
| Vnil : vector O
| Vcons : forall {n}, A -> vector n -> vector (S n).
Arguments Vcons {_} _ _.
Fixpoint countPV {n: nat} (v : vector n): nat :=
match v with
| Vnil => O
| Vcons x v' => if P_dec x then S (countPV v') …
Run Code Online (Sandbox Code Playgroud)