[英]Imitating placeholder behaviour using JavaScript
I am trying to re-create or immitate (HTML's) placeholder behaviour using javascript (and jQuery). 我正在尝试使用javascript(和jQuery)重新创建或模仿(HTML的)占位符行为。
Could anyone please tell me how I would achieve this? 谁能告诉我我将如何实现这一目标? I tried using the following code: 我尝试使用以下代码:
$(document).ready(function () {
//On init:
var initPlaceholderTxt = $("p").text();
//Execution:
//Using this, placeholder will not display correct information,
//lacking a previous clicked character:
$("#test").on("keydown", function (event) {
var value = $(this).val();
$("p").text(value);
});
/*
//Core feature:
$("#test").keyup(function() {
var value = $( this ).val();
if(value != null && value != "") {
$("p").text(value);
}
else {
$("p").text(initPlaceholderTxt);
}
});
//Make characters appear more dynamically or faster:
$("#test").keydown(function() {
var value = $( this ).val();
$("p").text(value);
});
*/
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea id="test"></textarea>
<p>Placeholder text</p>
<textarea placeholder="example"></textarea>
I got the main part working, however I am trying to make it more dynamic. 我的主要部分已经开始工作,但是我正在尝试使其更具活力。 When a user types something into a default input with a placeholder defined, the moment a key has been pressed down, a character will appear in the box and the placeholder will disappear. 当用户在定义了占位符的默认输入中键入内容时,按下键的瞬间,框中将出现一个字符,而占位符将消失。 This last part does not seem to happen with my current code. 我的当前代码似乎没有发生最后一部分。
As I press a key down, the placeholder already tries to change its text value before a new character has been added to the input. 当我按下一个键时,在将新字符添加到输入之前,占位符已经尝试更改其文本值。
Most would say that a solution to this would be the usage of keyup
. 大多数人会说解决方案是使用keyup
。 However there is a downside of using keyup
as well, as it is not making characters dynamically appear right of the bat when pressing a key. 但是,也有使用keyup
的缺点,因为它不能使按下键时字符动态显示在蝙蝠的右边。 Please let me know how to solve this. 请让我知道如何解决此问题。
Edit: The bottom text area is an example to show the intended behaviour. 编辑:底部文本区域是显示预期行为的示例。 The paragraph element is supposed to act/behave like (any) placeholder text which you see in the example. 段落元素应该像您在示例中看到的(任何)占位符文本一样/具有行为。
As I press a key down, the placeholder already tries to change its text value before a new character has been added to the input. 当我按下一个键时,在将新字符添加到输入之前,占位符已经尝试更改其文本值。
However there is a downside of using keyup as well, as it is not making characters dynamically appear right of the bat when pressing a key. 但是,也有使用键控的缺点,因为它不能使按下键时字符动态显示在蝙蝠的右边。
Then why not use both keyup
and keydown
? 那为什么不同时使用keyup
和keydown
呢?
You can use more than one event in a space separated list for the first argument of .on()
. 您可以在用空格分隔的列表中为.on()
的第一个参数使用多个事件。
EDIT 编辑
The bottom text area is an example to show the intended behaviour. 底部的文本区域是一个示例,用于显示预期的行为。
I edited my demo with an add/remove class which makes the second <textarea>
placeholder grey... Notice I given it an id
;) 我使用添加/删除类编辑了演示,该类使第二个<textarea>
占位符变为灰色...请注意,我给它指定了一个id
;)
$(document).ready(function() { //On init: var initPlaceholderTxt = $("p").text(); //Execution: $("#test").on("keyup keydown", function( event ) { var value = $( this ).val(); $("p").text(value); // Just for the testing, I assume... $("#mainTextArea").text(value).removeClass("placeholder"); // Restore the placeholder if the input is empty if(value==""){ $("p").text(initPlaceholderTxt); // Just for the testing, I assume... $("#mainTextArea").text(initPlaceholderTxt).addClass("placeholder"); } }); });
.placeholder{ color:grey; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="test"></textarea> <p>Placeholder text</p> <textarea placeholder="example" id="mainTextArea"></textarea>
A simple demo without the <p>
element and the placeholder
attribute: 一个没有<p>
元素和placeholder
属性的简单演示:
$(document).ready(function() { //On init: var initPlaceholderTxt = "Placeholder text"; $("#mainTextArea").text(initPlaceholderTxt).addClass("placeholder"); //Execution: $("#test").on("keyup keydown", function( event ) { var value = $( this ).val(); $("#mainTextArea").text(value).removeClass("placeholder"); // Restore the placeholder if the input is empty if(value==""){ $("#mainTextArea").text(initPlaceholderTxt).addClass("placeholder"); } }); });
.placeholder{ color:grey; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="test"></textarea> <br> <textarea id="mainTextArea"></textarea>
Hope this will work like charm.Thank you. 希望这会像魅力一样工作。谢谢。
var initPlaceholderTxt = $("p").text();
$("#test").on("keydown", function( event ) {
var value = $( this ).val();
$("p").text(value);
$("#desc").text(value);
});
Try this.You should check the input value length .Then apply placeholder as per your need 试试看。您应该检查输入值的长度。然后根据需要使用占位符
Add more event input keyup for more accuracy 添加更多事件输入键盘以提高准确性
$(document).ready(function() { var initPlaceholderTxt = $("p"); initPlaceholderTxt.text($(initPlaceholderTxt).attr('placeholder')) $("#test").on("keydown input keyup", function(event) { var value = $(this).val(); if ($.trim(value)) { initPlaceholderTxt.text("") } else { initPlaceholderTxt.text($(initPlaceholderTxt).attr('placeholder')); } }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="test"></textarea> <p placeholder="example"></p> <textarea placeholder="example"></textarea>
$(document).ready(function() { //On init: var initPlaceholderTxt = $("p").text(); $('#test').val('Text here'); $('#test').css('color','grey'); //Execution: $('#test').one('keypress',function(){ $('#test').val(''); $('#test').css('color','black'); }) //Using this, placeholder will not display correct information, //lacking a previous clicked character: $("#test").on("keyup", function( event ) { var value = $( this ).val(); $("p").text(value); }); /* //Core feature: $("#test").keyup(function() { var value = $( this ).val(); if(value != null && value != "") { $("p").text(value); } else { $("p").text(initPlaceholderTxt); } }); //Make characters appear more dynamically or faster: $("#test").keydown(function() { var value = $( this ).val(); $("p").text(value); }); */ });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea id="test" value="text"></textarea> <p>Placeholder text</p> <textarea placeholder="example"></textarea>
If you don't mind using jQuery UI you can take advantage of jQuery UI's position utility : 如果您不介意使用jQuery UI,则可以利用jQuery UI的position实用程序 :
Here's a general idea (if you want to make it a generic plugin): 这是一个总体思路(如果您想使其成为通用插件):
data-placeholder
attribute as a selector of the element which this element is a placeholder of. 我将data-placeholder
属性用作该元素占位符的元素的选择器。 originalText
在初始化期间,我将占位符文本保留在占位符数据originalText
placeholder
在初始化期间,我在textareas数据placeholder
保留对占位符元素的引用 //On init: $('[data-placeholder]').each(function () { var $parent = $($(this).attr('data-placeholder')); $(this).data('originalText', $(this).text()); $(this).position({ of: $parent, my: 'top left', at: 'top left' }); $parent.data('placeholder', $(this)); }); //Execution: //Using this, placeholder will not display correct information, //lacking a previous clicked character: $("#test").on("keyup", function( event ) { if ($(this).data('placeholder') && $(this).val()) { $(this).data('placeholder').text(''); } else { $(this).data('placeholder').text($(this).data('placeholder').data('originalText')); } });
[data-placeholder] { z-index: 0; pointer-events: none; height: 0; overflow: visible; color: rgba(0, 0, 0, 0.5); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> <div> <textarea id="test"></textarea> <span data-placeholder="#test">Placeholder text</span> </div> <div> <textarea placeholder="example"></textarea> </div>
I am just going to add the es6 javascript version because I really hope that will be needed in few years time as jQuery will be derelict. 我只是要添加es6 javascript版本,因为我真的希望在几年后将需要使用jQuery,因为jQuery将被废弃。
In a few words that's a function that takes 2 arguments: your input element and the placeholder you want to give to it, it will take care of all the rest meaning: 简而言之,该函数需要2个参数:输入元素和要赋予它的占位符,它将处理所有其余含义:
Surely is not optimised for performance but should get you going. 当然,不是针对性能进行优化的,但应该可以助您一臂之力。
const input = document.querySelector('input') const placeholderCustom = (input, placeholder) => { !input.value ? input.value = placeholder : '' input.addEventListener('focus', () => { input.value === placeholder ? input.value = '' : placeholder }) input.addEventListener('blur', () => { input.value === '' ? input.value = placeholder : '' }) input.addEventListener('input', () => { input.value === placeholder ? input.value = '' : '' }) } placeholderCustom(input, 'That is your placeholder')
<input type="text">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.