简体   繁体   English

HTML / JavaScript:如何制作a的默认文本 <textarea> 不可删除?

[英]HTML/JavaScript: How to make default text of a <textarea> undeletable?

Is there a way to make the default text of a <textarea> input become undeletable by the user? 有没有办法让<textarea>输入的默认文本被用户取消删除? For example, if we had a <textarea> like this: 例如,如果我们有这样的<textarea>

<textarea>I like/dislike this site because</textarea>

The user should be able to complete the "I like/dislike this site because" sentence but never be able to remove this part. 用户应该能够完成“我喜欢/不喜欢这个网站因为”句子,但永远无法删除这部分。

I understand there are easier approaches for this problem, but I've been asked to do it this way. 我知道这个问题有更简单的方法,但我被要求这样做。 Also, the target website uses prototype javascript framework so it must either involve prototype or no framework. 此外,目标网站使用原型javascript框架,因此它必须涉及原型或没有框架。 jQuery cannot be used as it would cause conflict with prototype. jQuery无法使用,因为它会导致与原型冲突。

Many thanks in advance. 提前谢谢了。

You could make it look like it's part of the textarea, even if it is not. 你可以让它看起来像是textarea的一部分,即使它不是。

Simple jsfiddle example to demonstrate the idea 简单的jsfiddle示例来演示这个想法

I would fake it. 我会假装它。 It's dirty but you cannot delete the default text: http://jsfiddle.net/2EMkF/3/ . 它很脏但你无法删除默认文本: http//jsfiddle.net/2EMkF/3/

function $(id) {return document.getElementById(id);}
$('s').addEventListener('click', function() {
    $('t').setSelectionRange(33, 33)
    $('t').focus();
});
$('t').addEventListener('keyup', function() {
    if($('t').value.substring(0, 33) != ' '.times(33)) {
            $('t').value = ' '.times(Math.max(
                33, $('t').value.firstspace())
            )
            + $('t').value.fromnospaces();
            $('t').setSelectionRange(33,33);
    }
});

function t() {
    if(this.selectionStart < 33) this.setSelectionRange(33,33);
}

$('t').addEventListener('keyup', t);
$('t').addEventListener('click', t);

String.prototype.times = function(n) {
    var r = "";
    for(var i = 0; i < n; i++) r += this;
    return r;
}

String.prototype.firstspace = function() {
    for(var i = 0; i < this.length; i++)
        if(this[i] != ' ')
            return i;
    return -1;
}

String.prototype.fromnospaces = function() {
    return this.substring(this.firstspace());
}

This code will throw the default text back into the beginning of the text field if it's not present after the user changes it: 如果用户更改后该代码不存在,此代码会将默认文本返回到文本字段的开头:

<script type="text/javascript">
function fixText()
{
    var textAreaElement = document.getElementById("mytextarea");
    var searchPhrase = "I like/dislike this site because";
    if(textAreaElement)
    {
        if(textAreaElement.value.indexOf(searchPhrase) != 0)
        {
            textAreaElement.value = searchPhrase + " " + textAreaElement.value
        }
    }
}
</script>

<textarea id="mytextarea" onblur="fixText();" onchange="fixText();">I like/dislike this site because</textarea>

I threw this together in just a minute or so, and it has some obvious flaws, such as the fact that if someone only deletes SOME of the default text they might be get a weird result like "I like/dislike this site because I like this site because it's nice". 我在一两分钟左右把它扔到一起,它有一些明显的缺陷,例如如果有人只删除了一些默认文本,他们可能会得到一个奇怪的结果,比如“我喜欢/不喜欢这个网站,因为我喜欢这个网站,因为它很好“。 But I'm sure you could manipulate it to make it more effective. 但我相信你可以操纵它以使其更有效。 Something like this might work in combination with pimvdb's answer. 像这样的东西可能与pimvdb的答案结合起来。

I like Elian Ebbing's answer though, if your employer will allow you to do it that way. 我喜欢Elian Ebbing的答案,如果你的雇主允许你这样做的话。

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

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