jQuery表单获取和失去焦点输入框提示效果的实例代码 - Web前端
作者:98wpeu发布时间:2026-10-07分类:网页前端技术浏览:74
导读:复制代码代码如下: $("#focus.input_txt").each(function(){ varthisVal=$(this).val(); //判断文本框的值是...
复制代码 代码如下:
$("#focus .input_txt").each(function(){
varthisVal=$(this).val();
//判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
//聚焦型输入框验证
$(this).focus(function(){
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
});
})
$("#keydown .input_txt").each(function(){
var thisVal=$(this).val();
//判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
$(this).keyup(function(){
var val=$(this).val();
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
})
})
})
</script>
<style type="text/CSS">
FORM{width:400px;margin:10px auto;border:solid 1px #E0DEDE;background:#FCF9EF;
padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}
label{display:block;height:40px;position:relative;margin:20px 0;}
span{position:absolute;float:left;line-height:40px;left:10px;color:#BCBCBC;cursor:text;}
.input_txt{width:398px;border:solid 1px #ccc;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;
height:38px;text-indent:10px;}
.input_txt:focus{box-shadow:0 0 4px rgba(255,153,164,0.8);border:solid 1px #B00000;}
.border_radius{border-radius:5px;color:#B00000;}
h2{font-family:"微软雅黑";text-shadow:1px 1px 3px #fff;}
</style>
</head>
<body>
<formclass="border_radius" id="focus">
<h2>聚焦型提示语消失 无效果请刷新</h2>
<label><span>花瓣注册邮箱</span><input type="text" class="input_txt border_radius" />
</label>
<label><span>密码</span><input type="text" class="input_txt border_radius" /></label>
</Form>
<form class="border_radius" id="keydown">
<h2>输入型提示语消失</h2>
<label><span>花瓣注册邮箱</span><input type="text" class="input_txt border_radius" />
</label>
<label><span>密码</span><input type="text" class="input_txt border_radius" /></label>
</form>
</body>
</html>
$("#focus .input_txt").each(function(){
varthisVal=$(this).val();
//判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
//聚焦型输入框验证
$(this).focus(function(){
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
});
})
$("#keydown .input_txt").each(function(){
var thisVal=$(this).val();
//判断文本框的值是否为空,有值的情况就隐藏提示语,没有值就显示
if(thisVal!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
$(this).keyup(function(){
var val=$(this).val();
$(this).siblings("span").hide();
}).blur(function(){
var val=$(this).val();
if(val!=""){
$(this).siblings("span").hide();
}else{
$(this).siblings("span").show();
}
})
})
})
</script>
<style type="text/CSS">
FORM{width:400px;margin:10px auto;border:solid 1px #E0DEDE;background:#FCF9EF;
padding:30px;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;}
label{display:block;height:40px;position:relative;margin:20px 0;}
span{position:absolute;float:left;line-height:40px;left:10px;color:#BCBCBC;cursor:text;}
.input_txt{width:398px;border:solid 1px #ccc;box-shadow:0 1px 10px rgba(0,0,0,0.1) inset;
height:38px;text-indent:10px;}
.input_txt:focus{box-shadow:0 0 4px rgba(255,153,164,0.8);border:solid 1px #B00000;}
.border_radius{border-radius:5px;color:#B00000;}
h2{font-family:"微软雅黑";text-shadow:1px 1px 3px #fff;}
</style>
</head>
<body>
<formclass="border_radius" id="focus">
<h2>聚焦型提示语消失 无效果请刷新</h2>
<label><span>花瓣注册邮箱</span><input type="text" class="input_txt border_radius" />
</label>
<label><span>密码</span><input type="text" class="input_txt border_radius" /></label>
</Form>
<form class="border_radius" id="keydown">
<h2>输入型提示语消失</h2>
<label><span>花瓣注册邮箱</span><input type="text" class="input_txt border_radius" />
</label>
<label><span>密码</span><input type="text" class="input_txt border_radius" /></label>
</form>
</body>
</html>
相关推荐
- 加速网页索引及其 SEO 效果的 10 种方法 - Web前端
- 用实例解析关于 vue3 setup store 的用法 - Web前端
- 前端开发:语法结构,写代码的「简洁度」差异 - Web前端
- 若需代码格式检查,再装eslint、prettier(已有可跳过) - Web前端
- 浏览器是如何一步步把代码变成网页的? - Web前端
- 微信小程序怎么实现分享成功?分享效果还能更出彩? - Web前端
- 小程序授权登录 Java 怎么实现?从流程到代码手把手讲清楚 - Web前端
- 用实例解析微信小程序中image的mode属性的基础知识和应用 - Web前端
- 微信小程序开发者工具提示error fail to open ide该怎么解决? - Web前端
- jquery.cookie.js 操作cookie实现记住密码功能的实现代码 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 9jQuery编写widget的一些技巧分享 - Web前端
- 最近发表
-
- WordPress 应用:如何修改模版头部文件中的 wp_head() - Web前端
- SEO 优化:YouTube SEO Plugin 提升影片流量 - Web前端
- 使用 React 服务器组件在 Next.js 13 中进行国际化 - Web前端
- 3 个意想不到的技术 SEO 问题示例(以及如何解决它们) - Web前端
- Vue 技术专题:Suspense、路由模式与单元测试实践 - Web前端
- 如何防止 WordPress SQL 注入攻击? - Web前端
- SEO 优化:如何围绕技术SEO框架以构建和制定策略? - Web前端
- SEO优化:YouTube SEO 关键词研究工具 - Web前端
- 2025 年帮助代理机构提升客户业绩的 14 款最佳 SEO 工具 - Web前端
- 客户端:服务器之间设置本地 DNS 服务器 - Web前端


