Jquery addClass 函数在 Firefox 中不起作用

标签 jquery css firefox

我在 jQuery addClass() 中遇到问题,它在 firefox 中不起作用,这意味着该类未添加到当前元素。以下内容适用于 Chrome 和 Safari(未在 IE 上测试)。

这是 CSS:

$(document).ready(function(){
				$(".placeholder").click(function(){
					$(this).addClass("placeholder-small");
					$(this).prev("input").focus();
				});
				$("input").on("focus",function(){
					$(this).nextAll(".placeholder").first().addClass("placeholder-small");
				});
			});
* {box-sizing:border-box;outline:none;-webkit-appearance:none}
			html, body {padding:0;margin:0;background-color:#131419;color:#fff;font-family:"DIN", arial;font-size:14px;letter-spacing:0.15em;font-weight:300}
			input[type="text"], input[type="email"], input[type=number], input[type="password"] {max-width:250px;width:100%;background-color:transparent;border-radius:0;border:0;border-bottom:1px solid #b6b6b8;color:#fff;font-family:"DIN",arial;font-size:inherit;padding:10px 8px;margin:10px 0;z-index:1}
			.required {position:relative}
			.required:after {position:absolute;top:0;right:0;content:"*";color:#ff0000;z-index:9}
			.placeholder {position:absolute;top:0;left:0;transition:all .2s;z-index:0}
			textarea ~ .placeholder {top:-135px}
			.placeholder-small, input:-webkit-autofill ~ .placeholder {transform:translateY(-125%);-moz-transform: translateY(-125%);font-size:0.85em;color:#616777}
<form method="post">
		<div style="padding:50px"><span class="required"><input type="text" name="firstname" value="" pattern=".{1,}" required minlength="1" maxlength="255" /><div class="placeholder">Prénom</div></span></div>
		</form>
		<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
		<script>
			$(document).ready(function(){
				$(".placeholder").click(function(){
					$(this).addClass("placeholder-small");
					$(this).prev("input").focus();
				});
				$("input").on("focus",function(){
					$(this).nextAll(".placeholder").first().addClass("placeholder-small");
				});
			});
		</script>

如果需要,您可以在此页面上实时查看它: https://www.goldenkeyz.com/fr/signup/

最佳答案

这是你的问题:

.placeholder-small, input:-webkit-autofill ~ .placeholder { ...

webkit 特定的声明导致 Firefox 上的解析失败,这是应该的。如果解析器遇到它无法识别的内容,它将忽略整个代码块:

The selector consists of everything up to (but not including) the first left curly brace ({). A selector always goes together with a {}-block. When a user agent can't parse the selector (i.e., it is not valid CSS3), it must ignore the {}-block as well.

如果您删除 webkit 部分,或将其分离到它自己的语句中,代码将按预期工作:

.placeholder-small {
    transform: translateY(-125%);
    font-size: 0.85em;
    color: #616777;
}

input:-webkit-autofill ~ .placeholder {
    transform: translateY(-125%);
    font-size: 0.85em;
    color: #616777;
}

请注意,您不需要使用 -moz-transform,从 2012 年的版本 16 开始,简单的转换就足够了。

关于Jquery addClass 函数在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58844806/

相关文章:

javascript - Select2 仅通过 id 设置选定值

css - Firefox 中负边距值的问题

html - 更改导航栏上的背景颜色时出现问题。

azure - Google Chrome 已阻止 azure.microsoft.com 域

jquery - ie7 中 jQuery append 问题

javascript - 无法使用 Jquery 隐藏 div 元素

jquery - 如何在 Require.JS 上下文中删除 jQuery "html"函数?

html - 如何垂直对齐 Bootstrap 导航栏品牌?

html - 火狐 :before ignores positions

jquery - 更新 $ ("body").css ("background") 导致 wpaudioplayer 刷新,适用于 Firefox 3.6 及以下版本