【问题标题】:React component event fire manually from client side browser console从客户端浏览器控制台手动触发 React 组件事件
【发布时间】:2016-12-06 07:21:53
【问题描述】:

在我的网站中,我使用 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事件

【问题讨论】:

    标签: javascript jquery reactjs dom-events google-chrome-devtools


    【解决方案1】:

    您可以从控制台调度事件

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

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

    2.创建事件

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

    3.改变值

    input.textContent = "TEST";
    

    4.dispatch 事件

    input.dispatchEvent(eventX)
    

    jsfiddle DEMO test

    【讨论】:

    • 你!是天才!!太感谢了!我认为这是不可能的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多