【发布时间】:2020-07-07 12:02:48
【问题描述】:
我正在编写一个 Angular 应用程序,它使用 ASP.Net Core 作为后端,并使用 bootstrap 4 和 ngx-bootstrap 进行样式设置。
由于某种原因,在我所有的 HTML <label> 标签上(在每个组件/html 文件中,并且即使我的 ID 和 Name 属性与 <label> 标签的“for”属性匹配),找不到 ID或认可。我通过 chrome dev tools lighthouse audit 运行 HTML 验证了这一点。以下是代码示例:
<div class="modal-header">
<h4 class="fa-pull-left modal-title" id="modalTitle">{{title}}</h4>
</div>
<div class="modal-body" id="modalBody">
<form id="modalForm">
<div class="form-group">
<label for="swName">Name</label>
<input id="swName" name="swName" [(ngModel)]="switchman.Name" class="form-control" type="text" />
</div>
<div class="form-group">
<label for="eid">EID</label>
<input [(ngModel)]="switchman.Eid" class="form-control" id="eid" name="eid" type="text" />
</div>
<div class="form-group">
<label for="homeOffice">Home Office</label>
<input [(ngModel)]="switchman.HomeOffice" class="form-control" id="homeOffice" name="homeOffice" type="text" />
</div>
<div class="modal-footer">
<button (click)="accept()" class="btn btn-success mr-auto" type="button">
Submit
</button>
<button (click)="reject()" class="btn btn-danger" type="button">
Cancel
</button>
</div>
以本节为例:
<form id="modalForm">
<div class="form-group">
<label for="swName">Name</label>
<input id="swName" name="swName" [(ngModel)]="switchman.Name" class="form-control" type="text" />
</div>
id="swName 的<input> 属性与<label> 标记的for="swName" 属性匹配,但它根本没有被应用,就好像id="swName" 不存在一样。
如何获取标签来识别输入标签的ID?
【问题讨论】:
-
我也会尝试不使用骆驼案。这不是 HTML 标准。尝试使用 _ 或 - 作为分隔符。
-
如果您在浏览器的开发 (F12) 工具中检查呈现的 HTML,您是否看到正确的
ids?每个id在页面中都是唯一的吗? -
在开发工具栏的左侧,有一个“选择元素”工具(矩形内的箭头),也可以用
Ctrl+Shift+C打开它(至少在Chrome和Firefox中)在 Windows 中)。单击页面中的目标元素以在开发工具窗口的“元素”或“检查器”选项卡中查看该元素。然后您可以检查 HTML 标记。 -
它们是独一无二的吗?如果将焦点放在开发工具窗口中,则可以按
Ctrl+F或Cmd+F在呈现的HTML 中搜索特定字符串,例如swName。 -
避免该问题的一种方法是在
label中包含input元素。然后将建立连接,而不依赖于id。
标签: html angular forms bootstrap-4 label