3 回答
TA贡献1155条经验 获得超0个赞
用于document.querySelctor()选择元素。你可以试试这个-
const span = document.querySelector('#error-msg');
const btn = document.querySelector('.f-btn');
if (span.classList.contains('hide')) {
btn.disabled = false;
} else {
btn.disabled = true;
}
<span id="error-msg" class="hide">Text</span>
<button class="f-btn">Try it</button>
TA贡献1833条经验 获得超4个赞
function myFunction(){
if ($("#error-msg").hasClass("hide")){
document.getElementsByClassName("f-btn")[0].disabled = false;
} else {
console.log('false');
document.getElementsByClassName("f-btn")[0].disabled = true;
}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span id="error-msg" class=>Text</span>
<button class="f-btn" onclick='myFunction()'>Try it</button>
TA贡献1829条经验 获得超9个赞
这是您的代码的一些更改及其在 span 禁用类“隐藏”时的工作 false
<!DOCTYPE html>
<html>
<body>
<span id="error-msg" class="hide qdasd">Text</span>
<button class="f-btn" onclick='myFunction()'>Try it</button>
<script>
function myFunction() {
var checkHide = document.getElementById("error-msg").classList.value
if (checkHide.includes("hide")){
document.getElementsByClassName("f-btn")[0].disabled = false;
console.log(document.getElementsByClassName("f-btn"))
} else {
document.getElementsClassName("f-btn")[0].disabled = true;
}}
</script>
</body>
</html>
添加回答
举报