以下是angularJs的兼容,运用指令和IE兼容



starterDirective.directive(‘placeholder’, [‘$compile’, function($compile) {
return {
restrict: ‘A’,
scope: {},
link: function(scope, ele, attr) {
console.log(ele.position().top);
var input = document.createElement(‘input’);
var isSupportPlaceholder = ‘placeholder’ in input;
if(!isSupportPlaceholder) {
var fakePlaceholder = angular.element(
‘ + attr[‘placeholder’] + ‘‘);
fakePlaceholder.on(‘click’, function(e) {
e.stopPropagation();
ele.focus();
});
fakePlaceholder.css({
‘text-align’: ‘left’,
‘line-height’: ‘34px’
});
scope.getElementPosition = function() {
return ele.position();
};
scope.$watch(scope.getElementPosition, function() {
fakePlaceholder.css({
‘top’: ele.position().top + ‘px’,
‘left’: ele.position().left + ‘px’
});
}, true);
scope.getElementHeight = function() {
return ele.outerHeight();
};
scope.$watch(scope.getElementHeight, function() {
fakePlaceholder.css(‘height’, ele.outerHeight() + ‘px’);
});
if(ele.css(‘font-size’)) {
fakePlaceholder.css(‘font-size’, ele.css(‘font-size’));
}
if(ele.css(‘width’)) {
fakePlaceholder.css(‘width’, ele.css(‘width’));
}
if(ele.css(‘text-indent’)) {
fakePlaceholder.css(‘text-indent’,
parseInt(ele.css(‘text-indent’)) +
parseInt(ele.css(‘border-left-width’))
);
}
if(ele.css(‘padding-left’)) {
fakePlaceholder.css(‘padding-left’, ele.css(‘padding-left’));
}
if(ele.css(‘margin-top’)) {
fakePlaceholder.css(‘margin-top’, ele.css(‘margin-top’));
}
ele.before(fakePlaceholder);
$compile(fakePlaceholder)(scope);
ele.on(‘focus’, function() {
fakePlaceholder.hide();
}).on(‘blur’, function() {
if(ele.val() === ‘’) {
fakePlaceholder.show();
}
});
scope.isElementVisible = function() {
return ele.is(‘:visible’);
};
scope.$watch(scope.isElementVisible, function() {
var displayVal = ele.is(‘:visible’) ? ‘block’ : ‘none’;
fakePlaceholder.css(‘display’, displayVal);
if(displayVal === ‘blcok’ && ele.val()) {
fakePlaceholder.hide();
}
});
scope.hasValue = function() {
return ele.val();
};
scope.$watch(scope.hasValue, function() {
if(ele.val()) {
fakePlaceholder.hide();
}
});
}
}
};
}]);



<!–[if lte IE 9]>
<script>
$(function(){
/placeholder兼容/
$(‘[placeholder]’).each(function(){
var defaultvalue=$(this).attr(“placeholder”);
var altercss=$(this).attr(“altercss”);
if($.trim($(this).val())===’’){
$(this).val(defaultvalue);
if(altercss){$(this).addClass(altercss);}
}
$(this).focus(function(){
if($(this).val()==defaultvalue){
$(this).val(‘’);
if(altercss){$(this).removeClass(altercss);}
}
}).blur(function(){
if($.trim($(this).val())===’’){
$(this).val(defaultvalue);
if(altercss){$(this).addClass(altercss);}
}
});
});
});
</script>
<![endif]–>

以上有自己工作总结,欢迎广大群众提意见。真诚向您学习!!!!!!