【发布时间】:2021-02-15 15:53:33
【问题描述】:
我正在寻求 JS 的帮助,以使占位符在选择时移动到输入框上方。我已经尝试了各种编码,这似乎是最接近结果的。
我已尝试应用以下 JS 脚本,但似乎抛出了错误。可能是 jquery 的错误。
如果您能提供任何帮助,将不胜感激。
$('input').focus(function(){
$(this).parents('.billing_first_name_field').addClass('focused');
});
$('input').blur(function(){
var inputValue = $(this).val();
if ( inputValue == "" ) {
$(this).parents('.billing_first_name_field').removeClass('focused');
} else {
$(this).addClass('filled');
}
})
这是我应用的 HTML 和 CSS。
.formRow {
position: relative;
width: 100%;
}
.formRow--item {
display: block;
width: 100%;
}
.input-text {
position: relative;
padding: 15px 20px 11px;
width: 100%;
outline: none;
border: solid 1px rgb(149, 152, 154);
border-radius: 4px;
color: rgb(44, 50, 53);
letter-spacing: .2px;
font-weight: 400;
font-size: 16px;
resize: none;
transition: all .2s ease;
}
.screen-reader-text {
position: relative;
display: block;
width: 100%;
}
.screen-reader-text.active {
.placeholder {
top: -5px;
background-color: #ffffff;
color: #fd999a;
text-transform: uppercase;
letter-spacing: .8px;
font-size: 11px;
line-height: 14px;
transform: translateY(0);
}
}
.screen-reader-text:not(:focus):not(:hover) {
& ~ .placeholder {
color: #fec8c9;
}
}
.input-text {
&:focus,
&:hover {
border-color: red;
}
}
.placeholder {
position: absolute;
top: 50%;
left: 10px;
display: block;
padding: 0 10px;
color: rgb(149, 152, 154);
white-space: nowrap;
letter-spacing: .2px;
font-weight: normal;
font-size: 16px;
transition: all, .2s;
transform: translateY(-50%);
pointer-events: none;
user-select: none;
}
<div class="woocommerce-billing-fields__field-wrapper">
<p class="form-row form-row-first validate-required validate-required" id="billing_first_name_field" data-priority="">
<label for="billing_first_name" class="screen-reader-text"><abbr class="required" title="required">
</label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text" name="billing_first_name" id="billing_first_name" placeholder="First name" value="" autocomplete="given-name">
</span>
</p>
非常感谢!
【问题讨论】:
-
是的,我一直在努力寻找它。尝试去这里codepen.io/ettzzi/pen/ZWgWKw
-
我希望这行得通。
-
:focus伪类与.focused不同。
标签: javascript jquery css wordpress woocommerce