首页 > 解决方案 > 有没有办法在 React Material 上触发 onPaste 事件在 Internet Explorer 中?

问题描述

我正在设计一个数据表,它需要提供能够粘贴从 excel 复制的数据行的功能。但是,onPaste 事件不会在 Internet Explorer 中触发。我能够实现这一点并在 chrome 中获取剪贴板数据。

<Table className={classes.table} aria-labelledby="tableTitle">
            <EnhancedTableHead
              numSelected={selected.length}
              order={order}
              orderBy={orderBy}
              onSelectAllClick={this.handleSelectAllClick}
              onRequestSort={this.handleRequestSort}
              rowCount={data.length}
            />
            <TableBody onPaste={event => this.handlePaste(ClipboardEvent)}>
              {stableSort(data, getSorting(order, orderBy))
                .map(n => {
                  const isSelected = this.isSelected(n.id);
                  return (
                    <TableRow
                      hover
                      onClick={event => this.handleClick(event, n.id)}
                      role="checkbox"
                      aria-checked={isSelected}
                      tabIndex={-1}
                      key={n.id}
                      selected={isSelected}
                    >
                      <TableCell padding="checkbox">
                        <Checkbox checked={isSelected} />
                      </TableCell>
                      <TableCell component="th" scope="row" padding="none">
                        {n.udf}
                      </TableCell>
                      <TableCell align="right">{n.ticker}</TableCell>
                      <TableCell align="right">{n.transType}</TableCell>
                      <TableCell align="right">{n.qty}</TableCell>
                      <TableCell align="right">{n.portfolio}</TableCell>
                    </TableRow>
                  );
                })}
              {emptyRows > 0 && (
                <TableRow style={{ height: 49 * emptyRows }}>
                  <TableCell colSpan={6} />
                </TableRow>
              )}
            </TableBody>
          </Table>

标签: javascriptreactjshtmlinternet-explorerweb

解决方案


如果您检查文档,您会发现支持粘贴事件并且 OnPaste 事件的兼容性未知。

我对 OnPaste 事件进行了测试,发现该事件在 Internet Explorer 中有效。

问题是 Internet Explorer 不支持剪贴板数据。

因此,您将无法使用它访问数据。

在此处输入图像描述

测试代码:

<!DOCTYPE html>
<html>
<body>

<input type="text" onpaste="myFunction()" value="Try to paste something in here" size="40">

<p id="demo"></p>

<script>
function myFunction() {
  document.getElementById("demo").innerHTML = "You pasted text!";
}
</script>

</body>
</html>

Internet Explorer 11 中的输出:

在此处输入图像描述

参考:

(1) HTMLElement​.onpaste

(2)元素:粘贴事件

作为替代方案,您可以尝试参考下面链接中的示例可能会帮助您解决问题。

跨浏览器 JavaScript 复制和粘贴


推荐阅读