- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
下面的 javascript 是一个 HTML5 Placeholder shim。它创建一个标签来模仿不支持它的浏览器中的占位符属性。它在所有本身不支持 placeholder
的浏览器中运行得非常漂亮。
我注意到 FireFox 和 IE 中的这个错误 - 当 @media 查询激活时,错误的占位符被正确放置,但它不会隐藏在焦点上。
有人有什么想法吗?
(function($) {
$.extend($,{ placeholder: {
browser_supported: function() {
return this._supported !== undefined ?
this._supported :
( this._supported = !!('placeholder' in $('<input type="text">')[0]) );
},
shim: function(opts) {
var config = {
color: '#888',
cls: 'placeholder',
lr_padding:4,
selector: 'input[placeholder], textarea[placeholder]'
};
$.extend(config,opts);
!this.browser_supported() && $(config.selector)._placeholder_shim(config);
}
}});
$.extend($.fn,{
_placeholder_shim: function(config) {
function calcPositionCss(target)
{
var op = $(target).offsetParent().offset();
var ot = $(target).offset();
return {
top: ot.top - op.top + ($(target).outerHeight() - $(target).height()) /2,
left: ot.left - op.left + config.lr_padding,
width: $(target).width() - config.lr_padding
};
}
return this.each(function() {
var $this = $(this);
if( $this.data('placeholder') ) {
var $ol = $this.data('placeholder');
$ol.css(calcPositionCss($this));
return true;
}
var possible_line_height = {};
if( !$this.is('textarea') && $this.css('height') != 'auto') {
possible_line_height = { lineHeight: $this.css('height'), whiteSpace: 'nowrap' };
}
var ol = $('<label />')
.text($this.attr('placeholder'))
.addClass(config.cls)
.css($.extend({
position:'absolute',
display: 'inline',
float:'none',
overflow:'hidden',
textAlign: 'left',
color: config.color,
cursor: 'text',
paddingTop: $this.css('padding-top'),
paddingLeft: $this.css('padding-left'),
fontSize: $this.css('font-size'),
fontFamily: $this.css('font-family'),
fontStyle: $this.css('font-style'),
fontWeight: $this.css('font-weight'),
textTransform: $this.css('text-transform'),
zIndex: 99
}, possible_line_height))
.css(calcPositionCss(this))
.attr('for', this.id)
.data('target',$this)
.click(function(){
$this.data('target').focus()
})
.insertBefore(this);
$this
.data('placeholder',ol)
.focus(function(){
ol.hide();
}).blur(function() {
ol[$this.val().length ? 'hide' : 'show']();
}).triggerHandler('blur');
$(window)
.resize(function() {
var $target = ol.data('target')
ol.css(calcPositionCss($target))
});
});
}
});
})(jQuery);
jQuery(document).add(window).bind('ready load', function() {
if (jQuery.placeholder) {
jQuery.placeholder.shim();
}
});
最佳答案
我知道这不是真正的答案,但您是否尝试过其他插件?有一个 million different ones .
这个在过去对我来说非常有效:
https://github.com/mathiasbynens/Placeholder-jQuery-Plugin
它使用与您展示的插件不同的方法。
关于javascript - 占位符垫片 - 被媒体查询窃听,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7483204/
我正在尝试使用 Intern 测试 Require.js 项目。我在测试中遇到错误,其中在加载 jQuery 插件时未定义 jQuery。我的加载器配置如下所示: loader: { // A
还没有很多浏览器支持 WebRTC,但是有很多很酷的 WebRTC 东西可以玩——PeerDB 等。 是否有任何类型的 shim 用于在浏览器中欺骗 WebRTC API(这样 PeerDB 之类的东
我是堆栈 overflow 的新手。 :-) 如何创建一个自动改变高度的 div 来填满所有空间?我试过 高度:“自动” 但不起作用... :( 例如: ... The height
Windows 能够应用 shims to mis-behaving applications .垫片用于拦截 API 调用并更改它。例如,垫片可用于: 修改传入参数 谎报返回值 改成别的名字 App
以下代码在 IE9+(以及 Firefox 和 Chrome)中播放声音。是否有一个库可以为 IE8 实现/填充缺失的功能(audio 元素和 play() 方法)? 我做不到mediaelement
我有以下指向 backbone.d.ts 定义的内容。 import Backbone = module("../../../dep/backbone/backbone"); export class
我们通常会检查 jQuery 是否从 CDN 加载,如果没有则回退到本地版本。 window.jQuery || document.write('') 我正在使用 twitter bootstrap
我有一些 qunit 测试设置来测试我广泛使用 requirejs 的代码。我使用 Chutzpah 来执行在 VS 中运行的测试。如果我在浏览器中运行测试而不是仅在 VS 中运行测试,则一切正常。它
我正在为 ECMAScript Internationalization API 寻找垫片.有人知道这样的项目吗? (即使它目前仍在进行中。) 最佳答案 是的,有一个适用于 ECMA-402 的 po
我已经使用 Cordova 创建了一个 Web 应用程序,我需要在后台显示实时摄像头流。 Cordova 的相机/视频 API 似乎只是打开 native 相机/视频应用程序,而不是返回实时相机数据。
我正在尝试关注 this example并利用 shim 删除对 WCF 服务调用的外部依赖,该服务调用是从我正在执行单元测试的方法中调用的。与示例不同,我使用类似于以下的代码即时生成我的 WCF 客
我的服务器运行的是 PHP 5.2。是否有一组垫片以便我可以在 PHP 5.2 中至少使用 PHP 5.3 的一些新功能?喜欢: 最佳答案 我记不起 5.3 和 5.4 带来的所有功能,但简短的回答
如何为 C 库编写自定义 Emscripten 垫片? Emscripten 为某些 C 库(例如 SDL 和 OpenAL)捆绑了垫片,但对于其他库,您将不得不自己动手。 我所说的 shim 是指要
Internet Explorer 没有实现 ArrayBuffer.prototype.slice .令人惊讶的是,他们 don't plan on implementing it any time
我在 Dart 中有 Angular2 应用程序(如此处 - https://angular.io/docs/dart/latest/quickstart.html )并从那里调用 javascrip
这个问题在这里已经有了答案: What is TypeScript and why would I use it in place of JavaScript? [closed] (5 个答案) 关
我有以下代码: requirejs.config({ shim: { 'underscore': { exports: '_' },
Internet Explorer 不支持“const”关键字。我可以使用垫片来检查是否支持“const”,如果不支持,则将其重新定义为 var 吗?我想如果它能强制保持恒定性就好了,也许可以使用 o
所以我使用 background-size:cover 来实现背景图像的预期效果,该背景图像可以缩放到它所应用的任何 div 大小,同时保持纵横比。为什么要使用这种方法?根据相关 WordPress
我是一名优秀的程序员,十分优秀!