Javascript 从 event.target 获取子元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48494416/
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 child element from event.target
提问by mikebrsv
event.targetreturns the DOM element an action was performed on. How do I get a specific child element from the output?
event.target返回执行操作的 DOM 元素。如何从输出中获取特定的子元素?
Here's my form (with Angular markup):
这是我的表单(带有 Angular 标记):
<form (ngSubmit)="onSubmit($event)" id="kekpek">
<div class="form-group">
<label>Example file input</label>
<input type="file" class="form-control-file" #fileupload [(ngModel)]="myFile" name="myFile" (change)="fileChange($event)"/>
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
It fires the function:
它触发函数:
onSubmit(event) {
console.log(event.target);
}
Which output the entire form. I need to access the inputelement in the onSubmit()function.
其中输出整个表单。我需要访问函数中的input元素onSubmit()。
回答by Chirag Ravindra
You can use the documentElement.querySelector()method.
您可以使用该documentElement.querySelector()方法。
function onSubmit(event){
console.log(event.target.querySelector('input'));
}
You can use any valid CSS query to get the element you need
您可以使用任何有效的 CSS 查询来获取您需要的元素

