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

Sur*_*ttu 2 javascript console jquery dom-events reactjs


在我的网站中,我使用 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
            }
Run Code Online (Sandbox Code Playgroud)

我正在尝试从客户端 chrome 浏览器控制台将内容设置为该元素,但它不触发onInput事件。

我的问题是有一种方法可以动态添加输入并调用onInput为元素附加的事件

Kok*_*lav 7

您可以从控制台调度事件

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

const input = document.querySelector('[contentEditable]');
Run Code Online (Sandbox Code Playgroud)

2.创建事件

const eventX = new Event('input', {bubbles: true});
Run Code Online (Sandbox Code Playgroud)

3.改变价值

input.textContent = "TEST";
Run Code Online (Sandbox Code Playgroud)

4.调度事件

input.dispatchEvent(eventX)
Run Code Online (Sandbox Code Playgroud)

jsfiddle 演示测试

在此处输入图片说明