首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Mootools支持IE 8-9中的'placeholder‘属性?

如何使用Mootools支持IE 8-9中的'placeholder‘属性?
EN

Stack Overflow用户
提问于 2012-05-31 06:21:36
回答 2查看 1.5K关注 0票数 0

其他人也问过这个问题,但答案都是针对jQuery的。

EN

回答 2

Stack Overflow用户

发布于 2012-05-31 09:19:04

首先,你应该使用Mootools插件'OverText‘http://mootools.net/docs/more/Forms/OverText,它基本上模拟了这个功能。它实际上可以在任何浏览器中工作,所以,如果你愿意的话,你可以使用它,完全忘记占位符属性。但是如果你想支持占位符,但是在老的浏览器上使用OverText,你可以这样做:

代码语言:javascript
复制
window.addEvent('domready', function() {
    // create a test element
    var test = new Element('input'); 
    // if it has 'placeholder' this browser supports it already so you can exit
    if(("placeholder" in test)) { return; } 
    // for older browsers, get all the inputs which you have assigned a 'placeholder' attribute to
    $$('input[placeholder]').each(function(el) { 
        // and create an overtext for them using the value of the placeholder attribute
        new OverText(el, { textOverride: el.get('placeholder') });
    });
});
票数 5
EN

Stack Overflow用户

发布于 2012-05-31 06:21:36

代码语言:javascript
复制
/**
 * IE doesn't support the placeholder attribute, but we can set the value attribute.
 */
if (Browser.Engines.trident()) {
  var elements = $$('input[placeholder]');
  elements.map(function(it) {
    if (!it.getAttribute('value')) {
      it.setAttribute('value', it.getAttribute('placeholder'));
      it.onfocus = function() {
        it.setAttribute('value', '');
        it.onfocus = null;
      }
    }
  });
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10825075

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档