Jquery 模仿';占位符';IE的HTML5属性

Jquery 模仿';占位符';IE的HTML5属性,jquery,html,placeholder,Jquery,Html,Placeholder,我知道有很多问题都有答案,但我的情况有点不同。我试图模拟“占位符”属性的确切行为,即- 1。文本保持聚焦状态 2。光标移动到文本的开头 3。无法通过鼠标的点击拖动或键盘的移动箭头选择文本 4。文本在按键或粘贴时消失 第五个明显是“本”文本在提交时被删除 我想我已经完成了“1”和“4”的第一部分,但是“2”、“3”和“4”的第二部分(onpaste)仍然是一个问题…:( 我的jquery到目前为止 $(document).ready(function(){ if(!Modernizr.input.

我知道有很多问题都有答案,但我的情况有点不同。我试图模拟“占位符”属性的确切行为,即-

1。文本保持聚焦状态

2。光标移动到文本的开头

3。无法通过鼠标的点击拖动或键盘的移动箭头选择文本

4。文本在按键或粘贴时消失

第五个明显是“本”文本在提交时被删除

我想我已经完成了“1”和“4”的第一部分,但是“2”、“3”和“4”的第二部分(onpaste)仍然是一个问题…:(

我的jquery到目前为止

$(document).ready(function(){
if(!Modernizr.input.placeholder){
    $('[placeholder]').keypress(function() {
      var input = $(this);
      if (input.val() == input.attr('placeholder')) {
        input.val('');
        input.removeClass('placeholder');
      }
    }).keyup(function() {
      var input = $(this);
      if (input.val() == '' || input.val() == input.attr('placeholder')) {
        input.addClass('placeholder');
        input.val(input.attr('placeholder'));
      }
    }).keyup();
    $('[placeholder]').parents('form').submit(function() {
      $(this).find('[placeholder]').each(function() {
        var input = $(this);
        if (input.val() == input.attr('placeholder')) {
          input.val('');
        }
      })
    });
    $('[placeholder]').focus(function() {
    var input = $(this);
    if (input.val() == input.attr('placeholder')) {
              //move the cursor to beginning
              //move the text unselectable
    }
});
}
});
第二次和第三次,我尝试使用onselectstart、setCursorPosition、setSelectionRange等,但似乎没有任何效果。对于onpaste,我想用按键绑定它,但现在不知道在W3学校做什么bcoz没有这样的事件属性:尽管在MSDN上它显示了:


因此,请帮助!

我建议使用另一种方法,使用CSS更改标签在输入上的位置,并从JS开始,仅在IE上,使标签可见

(function ($) {

    $.fn.placeholder = function (options) {

        var options = $.extend({}, $.fn.placeholder.defaults, options);

        //show <label @for> for IE 
        if ($.browser.msie && $.browser.version < 10)
        {
            return this.each(function () {

                var $this = $(this);            

                $("label[for=" + $this.attr('id') + "]").show();

                //hide placeholder on focus
                $this.focus(function () {
                    if (!$.trim($this.val())) {
                        $("label[for=" + $this.attr('id') + "]").hide();
                    };
                });

                //show placeholder if the input is empty
                $this.blur(function () {
                    if (!$.trim($this.val())) {
                        $("label[for=" + $this.attr('id') + "]").show();
                    };
                });
            });
        }
    };

    //expose defaults
    $.fn.placeholder.defaults = {

    };
})(jQuery);
(函数($){
$.fn.placeholder=函数(选项){
var options=$.extend({},$.fn.placeholder.defaults,options);
//为IE表演
如果($.browser.msie&&$.browser.version<10)
{
返回此。每个(函数(){
var$this=$(this);
$(“label[for=“+$this.attr('id')+”])show();
//隐藏焦点上的占位符
$this.focus(函数(){
if(!$.trim($this.val())){
$(“label[for=“+$this.attr('id')+”])).hide();
};
});
//如果输入为空,则显示占位符
$this.blur(函数(){
if(!$.trim($this.val())){
$(“label[for=“+$this.attr('id')+”])show();
};
});
});
}
};
//公开默认值
$.fn.placeholder.defaults={
};
})(jQuery);

我担心我是否可以尝试这种方法,因为“标签”已经被用于一些其他功能(评论、排序等)…但谢谢你。我一直在尝试实现中的所有这些点,并且取得了一些有限的成功。如果你管理得更好,请随时在GitHub上打开拉取请求!