对于其他浏览器,只需设置#input-element { color: red; }
也足以为输入的占位符文本着色 ( <input type="text" placeholder="foo" />
)。
但是,在 iOS 上并非如此;这需要以下内容(也许更合适,但仍然):
#input-element::-webkit-input-placeholder { color: red; }
好的,没问题。但是,我正在尝试使用 jQuery 执行此操作,但没有取得太大成功。我尝试了以下方法:
$("#input-element::-webkit-input-placeholder").css('color', 'red');
$("#input-element").css('::-webkit-input-placeholder', 'color: red'); // not according to .css documentation: I'm aware
然而,这些都没有任何改变。我试图避免将 ( $(head).append("<style>..");
样式直接附加到文档。
如何通过 jQuery 更改输入的占位符样式属性 – 保持 iOS 兼容性?
最佳答案
为什么不只切换 CSS 类?
CSS:
input[type=text]::-webkit-input-placeholder { color: red; }
.blue-pl::-webkit-input-placeholder {
color: blue !important;
}
JavaScript:
$('#input-element').click(function(){$(this).addClass('blue-pl'); });
这适用于我的 iOS 6 iPhone。
关于javascript - 通过 jQuery 更改 iOS 的输入占位符样式属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13806360/