简体   繁体   English

从 event.target 获取子元素

[英]Get child element from event.target

event.target returns the DOM element an action was performed on. event.target返回执行操作的 DOM 元素。 How do I get a specific child element from the output?如何从输出中获取特定的子元素?

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 input element in the onSubmit() function.我需要访问onSubmit()函数中的input元素。

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 查询来获取您需要的元素

This should help这应该有帮助

onSubmit(event) {
    console.log(event.target.firstChild.children[1]);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM