嘿,我只是想知道是否会有一个按钮来启动和停止一个突变观察者?是我的观察者,甚至是对的,这是我第一次使用观察者。
respawn = function() {
var buttons = document.getElementsByClassName('hud-respawn-btn')[0];
var life = document.getElementsByClassName('hud-health-bar-inner')[0];
var dead = 1;
spawns = function() {
buttons.attributes.class.value = 'hud-respawn-btn';
buttons.click();
};
var observer2 = new MutationObserver(function(mutations) {
mutations.forEach(function(mutationRecord) {
if (parseInt(mutations[0].target.style.width) < dead) {
console.log("it works")
spawns();
}
});
});
observer2.observe(life, {
attributes: true,
attributeFilter: ['style']
});
};
-
<button onclick=\"respawn();\">respawn</button>