繁体   English   中英

从客户端浏览器控制台手动响应组件事件触发

[英]React component event fire manually from client side browser console


在我的网站中,我使用React Js及其可编辑的元素创建了一个div元素。 如果用户编辑内容,它将触发onInput函数。

r.createElement("div", {
                dangerouslySetInnerHTML: {
                    __html: e
                },
                dir: "auto",
                ref: "input",
                spellCheck: this.props.spellCheck,
                "data-tab": this.props.editable ? 1 : null ,
                contentEditable: this.props.editable,
                className: t,
                onInput: this.onInput,
                onPaste: this.onPaste,
                onCut: this.onCut,
                onKeyUp: this.onKeyUp,
                onKeyPress: this.onKeyPress,
                onMouseDown: this.onMouseDown,
                onContextMenu: this.onContextMenu,
                onFocus: this.props.onFocus,
                onBlur: this.props.onBlur
            }

我正在尝试从客户端Chrome浏览器控制台将内容设置为该元素,并且其不会触发onInput事件。
我的问题是有一种方法可以动态添加输入并调用为元素附加的onInput事件

您可以从控制台调度事件

1.通过querySelector ..找到dom节点,或在“ Elements”选项卡中选择元素并使用$ 0;

const input = document.querySelector('[contentEditable]');

2.创建事件

const eventX = new Event('input', {bubbles: true});

3.改变价值

input.textContent = "TEST";

4.派遣事件

input.dispatchEvent(eventX)

jsfiddle演示测试

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM