1

HTML に相当する XHTML は何ですかplaceholder

4

2 に答える 2

2

HTML5プレースホルダープロパティで発生するように、xhtml + js + cssを使用してプレースホルダーをシミュレートする実験を行いました。

xhtml:

 <input id="textbox-obj" type="text" class="search-txt" data-placeholder="Search" title="Search something here">

Javascript (jquery):

 //Function to place the textbox cursor to the begining
 function moveCursorToStart(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = 0;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(true);
        range.select();
    }
 }

 function initSearchTextPlaceholder(textBox) {
    textBox.focus(function() {
      if( $(this).val() == $(this).attr('data-placeholder') ) {
        moveCursorToStart(this);

        // To fix Chrome's bug
        window.setTimeout(function() {
            moveCursorToStart(this);
        }, 1);
      }
    }).blur(function() {
      if( $(this).val() == $(this).attr('data-placeholder') || $(this).val() == '' ) {
        $(this).addClass('placeholder').val($(this).attr('data-placeholder'));
      }
    }).on('keypress', function() {
      if( $(this).val() == $(this).attr('data-placeholder') ) {
        $(this).removeClass('placeholder').val('');
      }
    }
    ).blur();
 }

 initSearchTextPlaceholder($("#textbox-obj"));

CSS

.search-txt {
    color: #333;
}
.search-txt.placeholder {
    color: #8d8d8d;
}
于 2014-10-16T10:44:16.740 に答える