删除小屏幕/移动设备上的输入占位符,但在调整为桌面大小时再次添加它们
Remove input placeholders on small screen / mobile but add them again when resized to desktop
HTML
<div class="container">
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<label class="hidden-lg hidden-md hidden-sm">Label 1</label>
<input type="text" placeholder="Placeholder 1" class="form-control" />
</div>
<div class="form-group">
<label class="hidden-lg hidden-md hidden-sm">Label 2</label>
<input type="text" placeholder="Placeholder 2" class="form-control" />
</div>
<div class="form-group">
<label class="hidden-lg hidden-md hidden-sm">Label 3</label>
<input type="text" placeholder="Placeholder 3" class="form-control" />
</div>
</div>
</div>
</div>
简讯
jQuery(document).ready(function () {
// Invoke Placeholder plugin to fix the IE9 placeholder issue
$('input, textarea').placeholder();
//Add placeholder on inputs on desktop
enquire.register("screen and (min-width: 767px)", function () {
$("input[placeholder]").each(function () {
$(this).attr("data-placeholder", this.placeholder);
$(this).bind("focus", function () {
this.placeholder = '';
});
$(this).bind("blur", function () {
this.placeholder = $(this).attr("data-placeholder");
});
});
});
// Remove placeholders completely for mobile
enquire.register("screen and (max-width: 766px)", function () {
$("input[placeholder]").each(function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
$(this).bind("focus", function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
});
$(this).bind("blur", function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
});
});
});
});
你好
在我的表单上,我需要输入字段上的占位符仅针对桌面显示,并在移动设备上删除。使用上述代码可以正常工作,除非用户重新调整其桌面浏览器窗口的大小。(即从大屏幕到小屏幕,然后再回到大屏幕!在这种情况下,占位符不会重新出现。
我正在使用这个占位符插件使占位符在较旧的浏览器中工作:https://github.com/mathiasbynens/jquery-placeholder
并查询.js根据屏幕尺寸添加/删除占位符。
非常感谢你能提供的任何帮助。
干杯莫里斯。
解决的小提琴 - 使用下面 Ganesh 的答案https://jsfiddle.net/MauriceT/v07ggLpe/23/
在我的代码样式标签中,您可以看到 540px 的媒体查询,当屏幕宽度为 540px 或小于占位符时将隐藏,当它增加时将出现 540px 占位符
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<style>
ul {
list-style-type: none;
}
ul li a {
color: green;
text-decoration: none;
padding: 3px;
display: block;
}
@media screen and (max-width: 540px) {
.form-control::-webkit-input-placeholder { /* WebKit browsers */
color: transparent;
}
.form-control:-moz-placeholder { /* Mozilla Firefox 4 to 18 */
color: transparent;
}
.form-control::-moz-placeholder { /* Mozilla Firefox 19+ */
color: transparent;
}
.form-control :-ms-input-placeholder { /* Internet Explorer 10+ */
color: transparent;
}
}
</style>
</head>
<body>
<div class="container">
<h2>Form control: input</h2>
<p>The form below contains two input elements; one of type text and one of type password:</p>
<form role="form">
<div class="form-group">
<label for="usr">Name:</label>
<input type="text" class="form-control" id="usr" placeholder="ganesh">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd">
</div>
</form>
</div>
</body>
</html>
试试这个:
请删除$("input[placeholder]")
以添加$("input[.form-control]")
jQuery(document).ready(function () {
// Invoke Placeholder plugin to fix the IE9 placeholder issue
$('input, textarea').placeholder();
//Add Remove placeholder on inputs
$("input[.form-control]").each(function () {
$(this).attr("data-placeholder", this.placeholder);
$(this).bind("focus", function () {
this.placeholder = '';
});
$(this).bind("blur", function () {
this.placeholder = $(this).attr("data-placeholder");
});
});
//Add placeholder on inputs on desktop
enquire.register("screen and (min-width: 767px)", function () {
$(".booking-engine-container input[placeholder]").each(function () {
$(this).attr("data-placeholder", this.placeholder);
$(this).bind("focus", function () {
this.placeholder = '';
});
$(this).bind("blur", function () {
this.placeholder = $(this).attr("data-placeholder");
});
});
});
// Remove placeholders completely for mobile
enquire.register("screen and (max-width: 766px)", function () {
$("input[placeholder]").each(function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
$(this).bind("focus", function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
});
$(this).bind("blur", function () {
$("input[placeholder]").removeAttr("data-placeholder");
$("input[placeholder]").removeAttr("placeholder");
});
});
});
});
在这里演示
希望这应该有效....
您可以尝试以下脚本来删除移动视图上的占位符。
<script>
var mql = window.matchMedia("screen and (max-width: 800px)")
if (mql.matches){ // if media query matches
$(':input').removeAttr('placeholder');
}
</script>
相关文章:
- 从桌面读取python文件时高亮显示代码
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- ExtJS——在展开/折叠时调整面板高度
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- Chrome应用程序调整窗口大小保持纵横比
- 如何自动调整标签的高度以适应内容
- 导航栏没有调整到浏览器屏幕的大小
- 调整屏幕大小后不显示子菜单
- 使用JavaScript根据窗口/视口的高度动态调整图像大小
- 合并两个数组,重新调整循环js
- Morris.js折线图x轴标签在调整大小后消失
- 调整缩放窗口高度提升缩放
- 调整屏幕大小时更改属性值
- 添加当窗口从移动设备更改为桌面时调整大小事件
- 移动与桌面滚动/调整大小问题
- 菜单文本在从移动设备调整到桌面后消失
- 移动设备和桌面之间的响应式导航调整大小问题
- 如何在Chrome桌面捕获中获取调整大小事件
- 删除小屏幕/移动设备上的输入占位符,但在调整为桌面大小时再次添加它们