event.target
返回执行操作的 DOM 元素。如何从输出中获取特定的子元素?
这是我的表单(带有 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>
它触发函数:
onSubmit(event) {
console.log(event.target);
}
输出整个表单。我需要访问 onSubmit()
函数中的 input
元素。
最佳答案
您可以使用 documentElement.querySelector()
方法。
function onSubmit(event){
console.log(event.target.querySelector('input'));
}
您可以使用任何有效的 CSS 查询来获取您需要的元素
关于javascript - 从 event.target 获取子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48494416/