![](/img/trans.png)
[英]How can I insert a variable from the React context API into a placeholder?
[英]How I can get the placeholder from an input in react?
问候...
我正在尝试编写一个测试,我需要从输入中获取占位符文本以验证这是否是预期的字符串。
例如:
const stringInput = () => $('#input_field')
我需要一种方法
stringInput().getPlaceholderText().includes('expected text')
感谢您的关注。
P/D:页面是用 React.js 构建的
存储对元素的引用,然后获取属性占位符。
const ref = useRef();
useEffect(() => {
const placeholder = ref.current.getAttribute("placeholder")
}, [ref])
return (
<input ref={ref}>
)
const input = document.getElementById("input"); const placeholder = input.getAttribute("placeholder"); console.log(placeholder);
<input type="text" placeholder="default value" id="input"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.