简体   繁体   English

我可以访问元素(输入标签),但不能访问 textContent 和 innerText 的内容

[英]i can access the element(input tag) but not its content for neither textContent nor innerText

 <div class="container"> <input id="word1" type="text" placeholder="Enter First Word"><br> <input id="submit1" type="submit" name="submit" value="check"> </div> <script type="text/javascript"> let word1 = document.getElementById('word1'); let submit = document.getElementById('submit1'); function display(){ alert(word1.textContent); alert(word1.innerText); alert(word1); } submit.addEventListener('click',display,false); </script>

the browser alerts with empty string for both textContent and innerText and i don't know why is this happening浏览器警告 textContent 和 innerText 均为空字符串,我不知道为什么会这样

input doesn't have textContent , if you want get it's value, you need to go with input.value . input 没有textContent ,如果要获取它的值,则需要使用input.value

 <div class="container"> <input id="word1" type="text" placeholder="Enter First Word"><br> <input id="submit1" type="submit" name="submit" value="check"> </div> <script type="text/javascript"> let word1 = document.getElementById('word1'); let submit = document.getElementById('submit1'); function display(){ alert(word1.value); } submit.addEventListener('click',display,false); </script>

暂无
暂无

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

相关问题 未捕获的错误:输入是一个空元素标签,既不能有`children`也不能使用`dangerouslySetInnerHTML` - Uncaught Error: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` ReactJs:输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJs: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` ReactJS 错误输入是一个空元素标签,不能有`children`也不能使用`dangerouslySetInnerHTML` - ReactJS Error input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML` 编写 JS 存储 10 个值但 TypeError: input is a void element tag and must not have `children` 也不能使用 `dangerouslySetInnerHTML - Writing JS to Store 10 Values But TypeError: input is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML 无法使用innerHTML 和innerText 更改标签内容 - Can't change tag content with innerHTML and innerText 该元素不会接受 textContent 输入 - The element will not take a textContent input jQuery 的 .text() 和 JavaScript 的 .innerText 都不起作用? - Neither jQuery's .text() nor JavaScripts .innerText works? Element.appendChild()在IE中使用了...解决方法? (与innerText和textContent相关) - Element.appendChild() hosed in IE .. workaround? (related to innerText vs textContent) 带有来自相机的输入的视频标签可在 iOS 上的 Safari 中运行,但既不能在 Chrome 中也不能在任何应用内浏览器中运行 - Video tag with input from Camera is Working in Safari on iOS but neither working in Chrome nor in any In-app Browser 如何获取出现在父标签之前的 textContent? - How can I get the textContent that appears before the parent tag?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM