简体   繁体   English

如何在html文本区域的开头添加默认文本?

[英]How do I add a default text to the beginning of an html text area?

Im building a personal little social network. 我正在建立一个个人的小社交网络。 As part of the design, all statuses should begin with a default text that shouldn't change, similar to "Hi, my name is…" and then the user finishes the rest. 作为设计的一部分,所有状态都应以不应更改的默认文本开头,类似于“嗨,我的名字是......”,然后用户完成剩下的工作。 Kind of like an HTML5 placeholder that doesn't go away. 有点像HTML5占位符,不会消失。 What would be the best way to accomplish this? 实现这一目标的最佳方法是什么?

Please refer this fiddle If it serves the purpose, then please find the code below. 如果它符合目的,请参考这个小提琴 ,请找到下面的代码。

Markup: 标记:

<textarea id='status'>
Hi, my name is...
</textarea>

JavaScript: JavaScript的:

document.querySelector('#status').addEventListener('input', function(e){
    var defaultText = 'Hi, my name is...',
        defaultTextLength = defaultText.length;
    if(this.selectionStart === this.selectionEnd && this.selectionStart defaultTextLength {
        this.value = defaultText;
    }
});

Note: For some dumb reason, I assumed jQuery (note that you do not need jQuery for this, but makes it a little easier). 注意:由于某些愚蠢的原因,我假设jQuery(请注意,您不需要 jQuery,但会让它更容易一些)。

Here is one solution uses a combination of text-indent and an absolutely positioned <span> (for the prefix part). 这里有一个解决方案使用text-indent和绝对定位的<span> (用于前缀部分)的组合。

Demo : http://jsfiddle.net/4YzZy/ 演示http//jsfiddle.net/4YzZy/


 $('textarea.withprefix').each(function() { var prefix = $('<span/>') .text($(this).data('prefix')) .addClass('prefix') .appendTo('body') .css({ left: $(this).position().left + 'px', top: $(this).position().top + 'px', }); $(this).css({textIndent: prefix.outerWidth() + 'px'}); }); 
 textarea, span.prefix { font-family: arial; /* change these as needed, but ensure that the textarea and the span use the same font */ font-size: 1em; font-weight: normal; padding: 2px; border-width: 1px; } span.prefix { position:absolute; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea class="withprefix" data-prefix="Hi There, "></textarea> 

If you want to use javascript to perform your task, you can set up some event listeners and then when the user makes a change, check to see if that change affected the text you want changed. 如果要使用javascript执行任务,可以设置一些事件侦听器,然后在用户进行更改时,检查该更改是否会影响您要更改的文本。 Sarbbottam's new code does this, but it will replace any text you have already typed if you modify the original text. Sarbbottam的新代码执行此操作,但如果您修改原始文本,它将替换您已输入的任何文本。 I have fixed this by saving the previous value of the textarea. 我通过保存textarea的先前值来解决这个问题。

HTML: HTML:

<textarea id="text">Hello, my name is </textarea>

Javascript: 使用Javascript:

var defaultText = "Hello, my name is ";
var valueOnKeyDown = new Array();
document.getElementById("text").addEventListener("keydown", function() {
    valueOnKeyDown.push(this.value);
}, false);
document.getElementById("text").addEventListener("keyup", function(e) {
    if(valueOnKeyDown[0].substring(0, defaultText.length) != this.value.substring(0, defaultText.length)) {
        this.value = valueOnKeyDown[0];
    }
    valueOnKeyDown = new Array();
}, false);

And of course a working demo . 当然还有一个工作演示

Another option that may work that is not mentioned in the other answers is to set up a keydown/keypress event listener and get the caret position in the textarea using javascript. 另一个可能有效的选项是在其他答案中没有提到的是设置keydown / keypress事件监听器并使用javascript在textarea中获取插入位置。 If it is less than the length of your default text, you just call event.preventDefault(). 如果它小于默认文本的长度,则只需调用event.preventDefault()。 The main disadvantage to this is that it may not be possible to make this completely cross-browser compatible. 这样做的主要缺点是可能无法使这种完全跨浏览器兼容。

If the goal is that the user cannot change this text then you can do a couple of things: 如果目标是用户无法更改此文本,那么您可以执行以下操作:

  1. add the text outside of the textarea (above it for instance) 在textarea之外添加文本(例如在它上面)
  2. Add the text outside of the textarea but place it behind the textarea using css. 在textarea外部添加文本,但使用css将其放在textarea后面。 You can make the textarea transparent so the user can see the text. 您可以使textarea透明,以便用户可以看到文本。 The problem there would be that the text the user types can fall over the text you placed in the background 问题在于用户输入的文本可能会落在您在后台放置的文本上
  3. Put it in a background image of the textarea 把它放在textarea的背景图片中
  4. Place the text inside the textarea ( <textarea>Hi, my name is </textarea> ) and use JavaScript to test for what has been typed and change the text if it changes into something that you do not want. 将文本放在textarea( <textarea>Hi, my name is </textarea> )中,并使用JavaScript来测试键入的内容,如果文本变成你不想要的内容,则更改文本。 There are masking plugins that you can use to do this. 您可以使用屏蔽插件来执行此操作。

The best options would be the first option and the forth. 最好的选择是第一个选项和第四个选项。 I'd go the first as it is by far the easiest 我是第一个,因为它是迄今为止最简单的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM