Javascript 如何根据元素的 ID 在 console.log 中打印值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27135846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print a value in console.log base on ID of an element?
提问by iori
<input id="email" name="email" type="text" value="[email protected]">
How do I get value of id=email and print it out to my console or anywhere for testing purposes ?
如何获取 id=email 的值并将其打印到我的控制台或任何地方以进行测试?
I tried, but no luck :(
我试过了,但没有运气:(
<script type="text/javascript">
document.getElementById('email').innerText;
</script>
回答by Ekansh Rastogi
There are two types of elements
有两种类型的元素
- Block - these can be accessed with -
.innerHTMLor.html()in jquery like div's span's etc - Non Block Elements - these can be accessed by
.valueor.val()in jquery, like input types etc
- 块 - 这些可以使用 -
.innerHTML或.html()在 jquery 中访问,如 div 的跨度等 - 非块元素 - 这些可以被jquery
.value或.val()在 jquery 中访问,如输入类型等
all you need o do is
你需要做的就是
console.log($('#email').val()) //jquery
or
console.log(document.getElementById('email').value); // javascript
回答by Yosoyke
try
尝试
console.log($('#email').val());
回答by EricBellDesigns
Try an alert...
尝试警报...
var email = $("#email").val();
alert(email);
回答by Brett Caswell
this will 'print' the entire object to the console
这会将整个对象“打印”到控制台
console.log("input[id='email'] - %o", document.getElementById('email'));
回答by xphong
Without jQuery:
没有 jQuery:
console.log(document.getElementById('email').value);
回答by chendianbuji
It works
有用
console.log(email.value)
回答by cjjenkinson
In the Chrome dev tools console drop the .value:
在 Chrome 开发工具控制台中删除 .value:
document.getElementById('#id')


