2016-11-03 6 views
-3

すべての変更(ユーザーとスクリプトの両方)をトリガーするイベントはありますか?私は2009年からDOM Mutationの使用を提案されたスタックの問題を発見しました。スクリプトによる入力変更のイベント(.value)

人類はこれに対してより良い解決策を発明しましたか?

P.S.状況を想像できない人のためのコード。

input = document.getElementById('input'); 
 
output = document.getElementById('output'); 
 
submit = document.getElementById('submit'); 
 

 
input.addEventListener('input', function(){ 
 
\t output.innerText = "It works!"; 
 
}) 
 

 
submit.onclick = function(){ 
 
\t input.value = 'Hello!'; 
 
} 
 

 
//Firstly try to Submit. Oh look! Nothing happened! 
 
//Now try to write anything. Hmm, it works!
<input id="input"> 
 
<button id="submit">Submit</button> 
 
<div id="output"></div>

+0

あなたが参照している_change_のどのような? – tmslnz

+0

いいえ、入力の値がJavascriptによって変更されたときにトリガーされるイベントはないと思います。関数を呼び出すために変更を行うコードを変更する必要があります。 – Barmar

+0

@tmslnzコードは次のとおりです。 – MaxelRus

答えて

0

人類はあなたがMutationObserverクラスを経由してDOMの変更のさまざまな観察することができ、変異イベント

をドロップするプロセスです。ストレート便宜上MDN(https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver)から

// select the target node 
 
var target = document.getElementById('SOME-ELEMENT-ID'); 
 
    
 
// create an observer instance 
 
var observer = new MutationObserver(function(mutations) { 
 
    mutations.forEach(function(mutation) { 
 
    console.log(mutation.type); 
 
    });  
 
}); 
 
    
 
// configuration of the observer: 
 
var config = { attributes: true, childList: true, characterData: true }; 
 
    
 
// pass in the target node, as well as the observer options 
 
observer.observe(target, config); 
 
    
 
// later, you can stop observing 
 
observer.disconnect();

関連する問題