javascript 获取文本框的输入类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6553327/
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
Get the input type of a textbox
提问by user782400
I am making a chrome extension for which I am trying to listen to the mouse clicks using message passing.
我正在制作一个 chrome 扩展,我试图使用消息传递来监听鼠标点击。
I want to know if it is possible to obtain the input type of a textbox when a mouse ic clicked on the textbox ?
我想知道当鼠标ic点击文本框时是否可以获取文本框的输入类型?
回答by Jim Schubert
The type of the input is a property on the element.
输入的类型是元素的一个属性。
For example, you can open chrome inspector on this page and type in the console:
例如,您可以在此页面上打开 chrome 检查器并在控制台中键入:
var firstInput = document.getElementsByTagName('input')[0];
firstInput.type; // outputs "text"
edit: you could bind the click on these elements and get the type from the event.target
property of the event.
编辑:您可以绑定对这些元素的点击并从event.target
事件的属性中获取类型。
回答by Acorn
What exactly are you having trouble with?
你到底有什么问题?
If you have the input element you can just do:
如果你有输入元素,你可以这样做:
theinputelement.getAttribute(type);
Update:
更新:
Okay, here's how you would get click events and find out the type of the input element with jQuery:
好的,这里是你如何获得点击事件并使用 jQuery 找出输入元素的类型:
$(document).click(function(event) {
if (event.which !== 1) {
return;
}
if (event.target.nodeName === 'INPUT') {
alert(event.target.type);
}
});
回答by jcomeau_ictx
have your onclick handler take an arg, and pass the object:
让您的 onclick 处理程序接受一个 arg,并传递对象:
<script>
function handler(object) {
alert(object);
}
</script>
<div onclick="handler(this);">this is a test</div>