如何在ReactJS中手动触发点击事件?

React.js

梅前端斯丁

2020-03-16

如何在ReactJS中手动触发click事件当用户单击element1时,我想自动触发对input标签的单击

<div className="div-margins logoContainer">
  <div id="element1" className="content" onClick={this.uploadLogoIcon}>
    <div className="logoBlank" />
  </div>
  <input accept="image/*" type="file" className="hide"/>
</div>

第1658篇《如何在ReactJS中手动触发点击事件?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

5个回答
西里达蒙 2020.03.16

您可以使用该ref道具通过回调获取对基础HTMLInputElement对象的引用,将该引用存储为类属性,然后使用该引用稍后使用HTMLElement.click方法触发事件处理程序中的单击

在您的render方法中:

<input ref={input => this.inputElement = input} ... />

在事件处理程序中:

this.inputElement.click();

完整示例:

class MyComponent extends React.Component {
  render() {
    return (
      <div onClick={this.handleClick}>
        <input ref={input => this.inputElement = input} />
      </div>
    );
  }

  handleClick = (e) => {
    this.inputElement.click();
  }
}

请注意ES6箭头函数,该函数this回调提供正确的词法作用域还要注意,您通过这种方式获取的对象类似于使用所获取的对象document.getElementById,即实际的DOM节点。

达蒙达蒙番长 2020.03.16

您可以使用ref回调函数来返回node调用click()该节点以编程方式单击。

获取div节点

clickDiv(el) {
  el.click()
}

设置一refdiv节点

<div 
  id="element1"
  className="content"
  ref={this.clickDiv}
  onClick={this.uploadLogoIcon}
>

检查小提琴

https://jsfiddle.net/pranesh_ravi/5skk51ap/1/

希望能帮助到你!

伽罗路易 2020.03.16

在功能组件中,该原理也适用,只是语法和思维方式略有不同。

const UploadsWindow = () => {
  // will hold a reference for our real input file
  let inputFile = '';

  // function to trigger our input file click
  const uploadClick = e => {
    e.preventDefault();
    inputFile.click();
    return false;
  };

  return (
    <>
      <input
        type="file"
        name="fileUpload"
        ref={input => {
          // assigns a reference so we can trigger it later
          inputFile = input;
        }}
        multiple
      />

      <a href="#" className="btn" onClick={uploadClick}>
        Add or Drag Attachments Here
      </a>
    </>
  )

}
泡芙Pro 2020.03.16

在2018年5月使用ES6 React Docs作为参考进行了以下操作:https : //reactjs.org/docs/refs-and-the-dom.html

import React, { Component } from "react";
class AddImage extends Component {
  constructor(props) {
    super(props);
    this.fileUpload = React.createRef();
    this.showFileUpload = this.showFileUpload.bind(this);
  }
  showFileUpload() {
    this.fileUpload.current.click();
  }
  render() {
    return (
      <div className="AddImage">
        <input
          type="file"
          id="my_file"
          style={{ display: "none" }}
          ref={this.fileUpload}
        />
        <input
          type="image"
          src="http://www.graphicssimplified.com/wp-content/uploads/2015/04/upload-cloud.png"
          width="30px"
          onClick={this.showFileUpload}
        />
      </div>
    );
  }
}
export default AddImage;
Eva神奇 2020.03.16

如果在最新版本的reactjs中不起作用,请尝试使用innerRef

class MyComponent extends React.Component {


  render() {
    return (
      <div onClick={this.handleClick}>
        <input innerRef={input => this.inputElement = input} />
      </div>
    );
  }

  handleClick = (e) => {
    this.inputElement.click();
  }
}

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android