动态检测html元素属性更改

时间:2016-11-09 23:58:32

标签: javascript jquery html

我有两个按钮 - A和B.如果A变为禁用/启用,我希望B始终相同。

在JavaScript中有一种简单的方法吗?

我们假设没有点击事件等。我只是想监视A和B的状态,并相应地改变。

1 个答案:

答案 0 :(得分:1)

// WITH CLICK ----------------------------------------------------
/*
$("#dissablebtnA").on("click", function() {
  $("#btnA").attr("disabled", "disabled");
  if ($("#btnA").attr("disabled") == "disabled") {
    console.log("dissabled button B because button A is disabled");
    $("#btnB").attr("disabled", "disabled");
  }

});
*/

// WITHOUT CLICK ----------------------------------------------------
/*
If you just want to monitor it without clicking you could do like this 
*/

// check every 5 seconds to see if it is dissabled and set b to dissabled accordingly
window.setInterval(function(){
  
 if ($("#btnA").attr("disabled") == "disabled") {
    console.log("dissabled button B because button A is disabled");
    $("#btnB").attr("disabled", "disabled");
   
  }
}, 5000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button id="btnA">button A</button>
<button id="btnB">button B</button>
<br>
<br>
<button id="dissablebtnA">DISABLE BUTTON A</button>