代码之家  ›  专栏  ›  技术社区  ›  mikebrsv

从事件中获取子元素。目标

  •  22
  • mikebrsv  · 技术社区  · 7 年前

    event.target 返回对其执行操作的DOM元素。如何从输出中获取特定的子元素?

    这是我的表格(带角度标记):

    <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);
    }
    

    输出整个表单。我需要访问 input 中的元素 onSubmit() 作用

    2 回复  |  直到 7 年前
        1
  •  36
  •   Chirag Ravindra    6 年前

    您可以使用 documentElement.querySelector() 方法

    function onSubmit(event){
        console.log(event.target.querySelector('input'));
    
    }
    

    您可以使用任何有效的CSS查询来获取所需的元素

        2
  •  0
  •   TWEEDOriginal    4 年前

    这应该会有帮助

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