繁体   English   中英

如何将鼠标拖过多个输入或带有 contentEditable 的元素到 select 里面的文本?

[英]How can I drag the mouse across multiple inputs or elements with contentEditable to select the text inside?

当 contentEditable 设置为 true 时,只能选择获得焦点的元素。 我正在尝试制作 function 的单元格,就像它们在 excel 中所做的那样。我希望能够通过单击编辑单元格,否则我只会将 contentEditable 设置为 state 和 select 作为常规 div 元素。 我想过用 onMouseOut 做些什么来将 state 更改为 false,但它们似乎不起作用。

应用组件:

import { useState, useRef, useEffect } from "react";
import Cell from "./Cell";

export default function App() {
  const testRef = useRef();
  const [st, setSt] = useState(false);

  useEffect(() => {
    if (st) testRef.current.focus();
  }, [st]);

  return (
    <div style={{ display: "flex" }}>
      <Cell tabindex="1" />
      <Cell tabindex="2" />
      <Cell tabindex="3" />
    </div>
  );
}

细胞成分:

import "./styles.css";
import { useState, useRef, useEffect } from "react";

export default function Cell() {
  const [editing, setEditing] = useState(true);

  const testRef = useRef();

  const stopEditing = () => setEditing(false);
  const startEditing = () => setEditing(true);
  return (
    <div>
      <div
        ref={testRef}
        contentEditable={editing}
        // onBlur={stopEditing}
        // onMouseUp={stopEditing}
        onMouseOver={startEditing}
        style={{
          display: "flex",
          border: "1px solid #696969",
          width: "100px",
          height: "25px",
          cursor: "cell",
          margin: "2px"
        }}
      ></div>
      <p>{editing.toString()}</p>
    </div>
  );
}

https://codesandbox.io/s/unruffled-rain-i0v40g?file=/src/Cell.js:0-734

<div class = "div1" onmousedown = "selectFunction()"> 

function selectFunction() { 

document.getElementByClassName("div1")[0].style.color = "red"; } 

也许是这样的? 但是,这仅适用于其中一个 div,请尝试将它们应用于所有 div。 这可能行不通,如果行不通,我将对其进行编辑。 (所有这一切都突出显示了红色的 div,如果你想一次 select 多个 div 可能不会工作)

如何使用 Javascript 来操作 ' <div contenteditable' from a website?< div><div id="text_translate"><p> 请帮忙,如何使用 javascript 更改(或获取)图像中的文本“19000”? 我需要知道它来自动填写一些登录网站等空白的任务。 谢谢你。 <a href="https://i.stack.imgur.com/DThmX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DThmX.jpg" alt="https://shopee.vn/buyer/login 这是一个我想抓取信息的网站。"></a></p><p> <a href="https://shopee.vn/buyer/login" rel="nofollow noreferrer">https://shopee.vn/buyer/login</a>这是我要爬取信息的网站。</p></div></div>

[英]How can I use Javascript to manipulate text inside '<div contenteditable' from a website?

暂无
暂无

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

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