简体   繁体   English

如何从 div 中获取文本?

[英]how can i get the text from within the div?


const questions = (props) => {
  const { question, options, id } = props.quiz;

  const selected = (e) => {
    e.target.children[0].checked = true;
    console.log(e.target);
  };

  return (
    <div className="bg-gray-200 p-6 m-10 rounded-md">
      <div className="font-bold text-xl text-slate-600">{question}</div>
      <div className="grid grid-cols-2 mt-4">
        {options.map((option) => (
          <div className="border-2 border-solid border-gray-400 rounded  text-gray-500 m-1">
            <div className="flex cursor-pointer p-3" id={id} onClick={selected}>
              <input type="radio" className="mr-2" name="name" />
              {option}
            </div>
          </div>
        ))}
      </div>
    </div>
  );
};

export default questions;

在此处输入图像描述

if console.log(e.target) is done in the selected function return the Div.如果 console.log(e.target) 在选定的 function 中完成,则返回 Div。 How can I access the text of the Div within the selected function?如何访问所选 function 中 Div 的文本?

You can access the text of the Div within the selected function by using the .textContent property on the e.target object. This will return the text content of the Div that was clicked.您可以使用e.target object 上的.textContent属性访问所选 function 内的 Div 的文本。这将返回被单击的 Div 的文本内容。

const selected = (e) => {
    e.target.children[0].checked = true;
    console.log(e.target.textContent);
  };

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

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