[英]How to fire change event of HTML element text using Javascript, in a Chrome Extension?
I'm creating an chrome extension, and having issue that even if I changed the element text, the submit button not enables as usual and other stuff is not processing as manual typing.我正在创建一个 chrome 扩展,并且有一个问题,即使我更改了元素文本,提交按钮也不会像往常一样启用,并且其他东西没有像手动输入一样处理。
I'm trying to fire the text changed event to process the elements normal behavior of manual typing using following script;我正在尝试触发文本更改事件以使用以下脚本处理手动键入的元素正常行为;
var el2=document.getElementById("tmsg");
el2.innerText="hello world";
try{
el2.fireEvent("onchange");
}catch(error)
{
alert("err:"+error);
}
However, the text is set, but getting following error但是,文本已设置,但出现以下错误
TypeError: el2.fireEvent is not a function
I'm looking for solution with java script, without JQuery solutions, but none worked for me.我正在寻找使用 java 脚本的解决方案,没有 JQuery 解决方案,但没有一个对我有用。
Can anyone please point me what I'm doing wrong here, or better way to do that?谁能指出我在这里做错了什么,或者更好的方法?
You are getting that error because eventTarget.fireEvent()
is a proprietary method used in Internet Explorer.您收到该错误是因为eventTarget.fireEvent()
是 Internet Explorer 中使用的专有方法。
The modern and standard way to manually dispatch events, according to MDN is as follows:根据MDN ,手动调度事件的现代和标准方法如下:
// CREATE EVENT
const event = new Event('change', {bubbles: true});
// GET TARGET ELEMENT
const el2 = document.getElementById('tmsg');
// DISPATCH EVENT
el2.dispatchEvent(event);
Hope it works.希望它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.