简体   繁体   English

如何在 Chrome 扩展中使用 Javascript 触发 HTML 元素文本的更改事件?

[英]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.

相关问题 Chrome扩展程序:在元素创建时触发事件? - Chrome Extension: fire an event when element created? 如何在 Chrome 扩展中的 javascript 中获取 HTML 元素 - How to get HTML element in javascript in Chrome Extension 如何在 Chrome 控制台中的 HTML 元素(如 tbody> tr > td)上触发 dbclick 事件? 我需要通过 javascript 代码来完成(用于过程自动化) - How to fire dbclick event on HTML element (like tbody> tr > td) in Chrome's Console? I need to do it by javascript code (for process automation) 收听元素的创建,并在Chrome扩展程序中显示在页面上时触发事件 - Listen for creation of an element and fire an event when it appears on the page in Chrome Extension 如何通过Chrome扩展程序更改Element Inspector工具提示/突出显示的文本? - How to change text of Element Inspector tooltip/highlight via Chrome Extension? 如何从Chrome扩展程序中插入的HTML元素调用Javascript函数? - How to call Javascript function from an inserted HTML element in a Chrome extension? 如何使用javascript从网页中删除HTML元素? (尝试进行Chrome扩展) - How do I remove an HTML element from a webpage using javascript? (Trying to make a chrome extension) 使用JavaScript更改值时如何触发TextBox更改事件? - How to fire TextBox change event when value is changed using javascript? Chrome扩展JavaScript,更改textarea中的选定文本 - Chrome extension javascript, change selected text in textarea 标签文本更改时如何触发事件 - how to fire event on label text change
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM