javascript - 如何在 javascript 函数中更改 span 内联元素的类?
问题描述
我正在尝试为具有眼睛图标(css class-fa fa-eye)的密码实现文本框。当用户单击眼睛图标时,它会变为斜线图标并将类型输入文本。
<div class="input-group show-hide-password">
<form:password id="form-control" path="password" class="form-control" required="" />
<div class="input-group-append">
<span id="PassSpan" class="input-group-text" onclick="typeChange('form-
control','PassSpan')">
<i class="fa fa-eye" aria-hidden="true"></i>
</span>
</div>
</div>
js文件
function typeChange(controlId, spanId) {
var x = document.getElementById(controlId);
if (x.type === "password")
{ x.type = "text";
}
else {
x.type = "password";
}
}
此功能仅更改输入类型,但我无法将类更改为 fa fa-eye-slash。fa-eye 和 fa-eye-slash 类存在于 css 文件中。但我不确定如何更改它在 span 元素内的类。
有人可以帮我吗。
解决方案
谢谢你们。我通过编写下面的新 js 函数解决了它,我在点击时调用它
function typeChange(controlId, spanId) {
var x = document.getElementById(controlId);
if ( spanId.classList.contains('fa-eye') ){
spanId.classList.toggle('fa-eye-slash');
}
if (x.type === "password")
{ x.type = "text";
}
else {
x.type = "password";
}
}
JSP
<i class="fa fa-eye" id ="passi" aria-hidden="true" onclick="typeChange('form-control',this)"></i>
推荐阅读
- ios - 仅当特定 ViewController 在前台时 iOS Swift 调用函数
- ios - 从 UITableView 中删除自定义视图
- c# - 'type' 是一个变量,但用作类型
- ssl - 如何为使用 nginx、gunicorn 和烧瓶的 GCP 实例设置 SSL 证书?
- ruby-on-rails - 在模型关系中指定不同的外键
- amazon-web-services - CloudFormation 没有执行我的命令?
- git - 从 master 连续 rebase 的 git 分支会导致重复提交
- python - 熊猫根据条件移动列数据
- php - 从mysql数据库unicode失败的Ajax搜索
- objective-c - 核心蓝牙 - 从 iOS(目标 C)在热敏打印机(ESC/POS)上打印图像的问题