jquery - 单击输入时将类添加到标签
问题描述
这是我有不同的跨度和每个跨度的一个输入和一个标签的东西。我想要的是当我单击输入时,它必须向标签添加一个类。
.in {position: relative; background: none}
.lb {position: absolute; color: #000; z-index: 1}
.outer {display: block; width: 100%; margin: 60px}
.color {color: green}
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text">
</span>
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text">
</span>
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text">
</span>
解决方案
$( "input" ).focus(function() {
$( this ).parent().find( "label").addClass("focus");
$( this ).parent().addClass(" focus-span");
});
$( "input" ).focusout(function() {
$( this ).parent().find( "label").removeClass("focus");
$( this ).parent().removeClass(" focus-span");
});
.in{position:relative; backgorund:none; }
.lb{position:absolute; color:#000; z-index:1;}
.outer{display:block; width:100%; margin:60px; }
.color{color:green;}
.focus{
color:red;
}
.focus-span{
border: 1px solid red;
}
<html>
<head>
<title>Page Title</title>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js"></script>
</head>
<body>
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text"/>
</span>
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text"/>
</span>
<span class="outer">
<label class="lb">hi there</label>
<input class="in" type="text"/>
</span>
</body>
</html>
恢复代码添加焦点也添加一些CSS来查看更改希望你能找到你的解决方案
推荐阅读
- c# - 如何将流媒体内容上传到 Azure?
- node.js - Strongloop oracle loopback 连接器是否支持 Oracle DB 加密策略?
- regex - 如何用正则表达式替换字符串中不需要的值
- angular - 在 Ionic 4 中导入幻灯片时显示错误
- gitlab - GitLab CI 缓存密钥
- reactjs - 启动使用 Create-react-app 创建的基本反应应用程序时出错
- mysql - MySQL通过左连接获得总和
- node.js - 将html页面发送给客户端
- computer-vision - 跟踪中track和tracklet有什么区别?
- c# - 如何使用 C# 查找正在执行的 Microsoft.NETCore.App 版本