此问题在此处已有答案:
Which equals operator (== vs ===) should be used in JavaScript comparisons?(48个答案)
How can I get the values of data attributes in JavaScript code?(11个答案)
What is the difference between the =
and ==
operators and what is ===
? (Single, double, and triple equals)(5个答案)
昨天关门了。
这段代码的问题是,我想得到我的数据属性的值,并知道它是否工作,我使用alert函数来看看它是否得到它。现在问题来了,每次我在else if条件中调用不同的数据属性时,它都显示null值。有人能解释一下如何做到这一点,以及为什么它是这样工作的吗?
function showDetails(value) {
let atr = value.getAttribute("data-Number");
let opr = value.getAttribute("data-clear");
if (value = atr) {
console.log("atr",atr);
} else {
console.log("opr",opr);
}
}
<div class="col border border-6">
<div class="btn-group btn-group-lg" role="group" aria-label="Large button group">
<button onclick="showDetails(this)" data-Number="1" type="button" class="btn btn-outline-dark">1</button>
<button onclick="showDetails(this)" data-Number="2" type="button" class="btn btn-outline-dark">2</button>
<button onclick="showDetails(this)" data-Number="3" type="button" class="btn btn-outline-dark">3</button>
<button onclick="showDetails(this)" data-operator="1" type="button" class="btn btn-outline-dark">+</button>
</div>
</div>
1条答案
按热度按时间wrrgggsh1#
若要解决此问题,应在if语句中使用比较运算符(==或===),如下所示: