angular自定义指令解决IE89不支持input的placeholder属性
下面代码实测通过,直接copy到本地运行即可。
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script src="https://cdn.bootcss.com/angular.js/1.2.14/angular.min.js"></script>
<style>
.yf-input-container {
position: relative;
font-size: 14px;
margin-bottom: 10px;
} .yf-input-container input {
height: 20px;
line-height: 20px;
}
</style>
</head> <body>
<div ng-controller="superPoolCtrl">
<div class="yf-input-container">
姓名:<input type="text" ng-model="name" yf-placeholder="请输入姓名" />
</div>
<div class="yf-input-container">
电话:<input type="text" ng-model="tel" yf-placeholder="请输入电话" />
</div>
<div>
<div class="yf-input-container">
职位:<input type="text" ng-model="job" yf-placeholder="请输入职位" />
</div>
</div>
</div>
<script>
var moduleName = 'bidmeApp';
var app = angular.module(moduleName, [])
.controller('superPoolCtrl', ['$scope', '$rootScope', '$timeout', function($scope, $rootScope, $timeout) {
$timeout(function() {
$scope.tel = "13800138000";
}, 1000);
$scope.name = "http://www.cnblogs.com/chenchenghua/";
}])
.directive('yfPlaceholder', function() {
return {
restrict: 'A', //指令取属性
scope: false, //与父级共享作用域
replace: true,
require: '',
template: function(elem, attr) {
return '<input />'
},
link: function(scope, element, attr) {
var inputEle = element[0];
var supportPlaceholder = "placeholder" in document.createElement("input") ? true : false;
if(supportPlaceholder) {
inputEle.setAttribute('placeholder', attr.yfPlaceholder);
} else {
function insertAfter(newElement, targetElement) { // newElement是要追加的元素 targetElement 是指定元素的位置
var parent = targetElement.parentNode; // 找到指定元素的父节点
if(parent.lastChild == targetElement) { // 判断指定元素的是否是节点中的最后一个位置 如果是的话就直接使用appendChild方法
parent.appendChild(newElement, targetElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
};
};
var node = angular.element(document.createElement('span'));
node.css({
position: 'absolute',
zIndex: '1',
color: '#A9A9A9',
top: inputEle.offsetTop + 'px',
left: inputEle.offsetLeft + 'px',
width: inputEle.offsetWidth + 'px',
height: inputEle.offsetHeight + 'px',
lineHeight: inputEle.offsetHeight + 'px',
textIndent: '5px',
cursor: 'text'
}).text(attr.yfPlaceholder);
angular.element(inputEle).after(node); node.on('click', function(a, b, c) { //点击placeholder,隐藏。input获取焦点
node.css({
"display": 'none'
});
inputEle.focus();
}); angular.element(inputEle).on('blur', function() { //input失去焦点时,做判断
if(inputEle.value.length < 1) {
node.css({
"display": 'block'
});
}
}); angular.element(inputEle).on('focus', function() { //input失去焦点时,做判断
if(inputEle.value.length < 1) {
node.css({
"display": 'none'
});
}
}); scope.$watch(attr.ngModel, function(newVal, oldVal) { //监听,比如开始有值或者异步请求回显,placeholder隐藏
if(!!newVal && newVal.length > 0) {
node.css({
"display": 'none'
});
}
});
}
}
};
});
angular.bootstrap(document.body, [moduleName]);
</script>
</body> </html>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎转载,转载请注明作者:飘飞的夏秋 和出处 http://www.cnblogs.com/chenchenghua/p/6757736.html
angular自定义指令解决IE89不支持input的placeholder属性的更多相关文章
- Angular自定义指令(directive)
angular自定义指令,意我们可以通过angula自己定义指令,来实现我们的特殊要求,为所欲为,一支穿云箭,千军万马来相见 多少年的老规矩了,先看代码: <!DOCTYPE html> ...
- angular 自定义指令详解 Directive
在angular中,Directive,自定义指令的学习,可以更好的理解angular指令的原理,当angular的指令不能满足你的需求的时候,嘿嘿,你就可以来看看这篇文章,自定义自己的指令,可以满足 ...
- 让IE下支持Html5的placeholder属性
HTML5对Web Form做了许多增强,比如input新增的type类型.Form Validation等. Placeholder 是HTML5新增的另一个属性,当input或者textarea设 ...
- Hmtl5 <input>中placeholder属性(新属性)
Hmtl5 <input>中placeholder属性(新属性) 一.定义和用法 placeholder 属性提供可描述输入字段预期值的提示信息(hint). 该提示会在输入字段为空时显示 ...
- Angular17 Angular自定义指令
1 什么是HTML HTML文档就是一个纯文本文件,该文件包含了HTML元素.CSS样式以及JavaScript代码:HTML元素是由标签呈现,浏览器会为每个标签创建带有属性的DOM对象,浏览器通过渲 ...
- 【vuejs深入一】深入学习vue指令,自定义指令解决开发痛点
写在前面 一个好的架构需要经过血与火的历练,一个好的工程师需要经过无数项目的摧残. 最近博主我沉淀了几个月,或者说懒了几个月.然而大佬的指点总是一针见血,能够让人看到方向.所以我现在有觉得,一个好的 ...
- 深入学习vue指令,自定义指令解决开发痛点
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code v-model指令 vue.js的定义是一个mvvm框架,将它发挥到极致能够极大的提升 ...
- Angular自定义指令directive:scope属性
在AngularJS中,除了内置指令如ng-click等,我们还可以自定义指令.自定义指令,是为了扩展DOM元素的功能.代码中,通过指定directive中的restrict属性,来决定这个指令是作为 ...
- angular自定义指令命名的那个坑
Directive 先从定义一个简单的指令开始. 定义一个指令本质上是在HTML中通过元素.属性.类或注释来添加功能.AngularJS的内置指令都是以ng开头,如果想自定义指令,建议自定义一个前缀代 ...
随机推荐
- java se系列(十二)集合
1.集合 1.1.什么是集合 存储对象的容器,面向对象语言对事物的体现,都是以对象的形式来体现的,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式.集合的出现就是为了持有对象.集 ...
- android 报错: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/animation/AnimatorCompatHelper;
在使用SmartRefreshLayout时,报 java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/a ...
- linux下定时任务的工具crontab的用法
Linux计划任务工具cron用法详解 linux下大名鼎鼎的计划任务工具crontab的使用介绍baidu.google上多得让人眼花缭乱,本着“天下文章一大抄”的觉悟,加上本人日常工作中总结的使用 ...
- Rails 中 mattr_accessor 一处文档错误
http://xiewenwei.github.io/blog/2015/01/11/mattr-accessor-in-ruby-on-rails-activesupport/ module Hai ...
- HTML5在线状态检测和DOM Storage
除了离线资源缓存外,html5离线应用开发还可能用到以下技术 在线状态检测 navigator.onLine navigator.onLine 属性表示当前是否在线.如果为 true, 表示在线:如果 ...
- Gradient Optimization
Gradient Optimization Gradient Descent Batch Gradient Descent Mini-Batch Gradient Descent Stochastic ...
- ReSharper+Devexpress 删除光标之后的换行
echo. >"$(ProjectDir)\Properties\licenses.licx" 官方链接
- 使用C#委托来实现异步编程
最近在我参与的几个.Net项目中都有用到异步编程,作为一名.Net小白,很有必要好好地学习一下异步编程. 什么是异步编程 异步编程指的就是不用阻塞当前线程来等待任务的完成,而是将任务扔到线程池中去执行 ...
- html中行级元素的居中显示。
垂直居中.以label标签为例. <style> #label1{ vertical-align:middle; line-height:40px;<*父元素的height*> ...
- WP手机短信导出方法和MSG格式文件阅读器的实现
最近想起来自己一直扔在抽屉里的Nokia920T里还存着珍贵的短信,觉得把它导出来存到电脑上比较稳妥也方便阅读.经过搜索找到一下方法:到应用市场里搜索contacts+message backup,安 ...