【问题标题】:How to align text inside a placeholder如何在占位符内对齐文本
【发布时间】:2018-10-05 23:07:36
【问题描述】:

我有一个输入类型的文本,其中占位符有两个值,例如(英语和西班牙语)。 但我希望英文文本左对齐,西班牙语文本应该右对齐,但在我的情况下,我正在这样做。

Example:

<input type="text" nam="name" placeholder="Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Nombre" >

我正在使用 nbsp 这样做,但是当我在另一个窗口上打开它时 或在响应模式下,文本超出或未对齐。 那么如何将占位符内的文本左右对齐,请帮助我与此相关..

【问题讨论】:

  • &lt;input type="text" nam="name" placeholder="Name Nombre" &gt;使用空间而不是&amp;nbsp;
  • 我想要它们之间的空间..这就是我遇到这个问题的原因
  • 不要——不要——使用placeholder。这是糟糕的用户体验。人们看不到他们在哪里输入。开始打字后。使用label

标签: jquery html css


【解决方案1】:

你不能用纯 html 和 css 做到这一点,解决方法是使用 js,并用 css 伪造占位符行​​为.. 就像示例一样..

var input = document.getElementById("name");

input.onblur = function() {
   if(!input.value) input.classList.remove('not-empty');
};

input.onfocus = function() {
   input.classList.add('not-empty');
};
.input-container {
    position: relative;
    display: inline-block;
}

.input-container:before {
    content: "name";
    position: absolute;
    left: 5px;
    top: 0;
    z-index: -1;
}

.input-container:after {
    content: "nombre";
    position: absolute;
    top: 0;
    right: 5px;
    z-index: -1;
}

.input-container input {
    z-index: 1;
    background: transparent;
}

.input-container input.not-empty {
    background: white;
}
&lt;div class="input-container"&gt;&lt;input id="name" type="text" name="name"&gt;&lt;/div&gt;

【讨论】:

    【解决方案2】:

    修罗提

    检查解决方案

    If you satisfied please give me vote as a gift :)
    

    $(document).ready(function(){
    		$("input").click(function(){
    			$("label").hide();
    		});
    		$("input").focusout(function(){
            	if($(this).val() !=''){
    				
    				} else{
    					$("label").show();
    				}
    				
    				
        	});
    	});
    p{position: relative; display: inline-block;}
    	label[for="english"] { position: absolute;left: 4px;}
    	label[for="spanish"] {position: absolute; right: 4px;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <p>
    <label for="english">Name </label>
    <label for="spanish">Nombre</label>
    <input type="text" nam="name">
    </p>

    【讨论】:

      【解决方案3】:

      我认为最干净的方法是:

      $(document).ready(function() {
        $('input').focus(function() {
          $('label').addClass("focus");
        })
        $('input').blur(function() {
          if(!$(this).val()) {
            $('label').removeClass("focus");
          }
        })
      });
      .input-group {
        position: relative;
        display: inline-block;
      }
      
      label {
        pointer-events: none;
      }
      
      label.focus {
        display: none;
      }
      
      label.english {
        position: absolute;
        left: 4px;
      }
      
      label.spanish {
        position: absolute;
        right: 4px;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="input-group">
        <label class="english" for="name">Name</label>
        <label class="spanish" for="name">Nombre</label>
        <input id="name" type="text" name="name">
      </div>

      Why you should use label ?
      & Some docs about multiple label

      【讨论】:

      • 如果你打算这样做,你应该在label 上设置一个规则,将指针事件属性值设置为无。如果没有这个,您必须在标签之间的空白处单击,这有点糟糕的用户体验。
      • 您应该添加 js 代码以将 labels 的可见性更改为在输入具有焦点且其中包含文本时隐藏。
      • 这是对标签上for属性的滥用...for应该指向与标签关联的表单元素的id属性。
      • 最后一个。在输入的模糊事件处理程序上,如果条件检查输入元素内是否没有文本,则将类的删除放在 if 条件中,然后才应该删除 .focus 类:if(!$(this).val())
      猜你喜欢
      • 2016-04-27
      • 2013-12-21
      • 2017-03-24
      • 2016-09-24
      • 2011-10-07
      • 2014-10-29
      • 2015-04-08
      • 2015-12-31
      相关资源
      最近更新 更多