原生js触发react change事件
var input=document.querySelector('input[placeholder="输入标题"]'); //For React 16 and React >=15.6 var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, "value").set; nativeInputValueSetter.call(input, 'react 16 value'); var ev2 = new Event('input', { bubbles: true}); input.dispatchEvent(ev2); //Outdated answer only for React <=15.5 var ev = new Event('input', { bubbles: true}); ev.simulated = true; element.value = 'Something new'; element.dispatchEvent(ev);
如果是textarea需要使用HTMLTextAreaElement