Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:基于文本长度的文本内容_Javascript - Fatal编程技术网

Javascript:基于文本长度的文本内容

Javascript:基于文本长度的文本内容,javascript,Javascript,大家好(第一张海报,所以现在不要拍我): 我正在尝试根据textlength更改textarea 我的代码如下: <script type="text/javascript"> function addComment(){ var commentBank = document.getElementById("commentBank"); var selectedComment = commentB

大家好(第一张海报,所以现在不要拍我):

我正在尝试根据textlength更改textarea

我的代码如下:

<script type="text/javascript">
            function addComment(){
                var commentBank = document.getElementById("commentBank");
                var selectedComment = commentBank.options[commentBank.selectedIndex].text;
                var currentComment = document.getElementById("comment");

                console.log(currentComment.textLength);

                if(currentComment.textContent == "Add comment here" ){
                    currentComment.textContent = selectedComment;
                }else if(currentComment.textLength == 0){
                    console.log("Trying to add "+selectedComment);
                    currentComment.textContent = "selectedComment";
                }
                else{
                currentComment.textContent += ". " + selectedComment;
                }


            }
            </script>

函数addComment(){
var commentBank=document.getElementById(“commentBank”);
var selectedComment=commentBank.options[commentBank.selectedIndex].text;
var currentComment=document.getElementById(“注释”);
console.log(currentComment.textLength);
if(currentComment.textContent==“在此处添加注释”){
currentComment.textContent=selectedComment;
}else if(currentComment.textLength==0){
log(“正在尝试添加”+selectedComment);
currentComment.textContent=“selectedComment”;
}
否则{
currentComment.textContent+=“+”selectedComment;
}
}
我试图做的是从一个选项(称为commentBank)中获取所选项目,从中选择文本并将其添加到一个文本区域(称为comment)

目前的问题是,当currentComment.textContent()等于0时,它不会将文本添加到textarea。 它会输出到日志,但是不会更新textarea来添加文本,因此if语句可以工作

有什么想法吗?
高级版谢谢。

这是您正在谈论的代码片段:

        }else if(currentComment.textLength == 0){
            console.log("Trying to add "+selectedComment);
            currentComment.textContent = "selectedComment";
它没有实际添加注释的原因是您正在将
currentComment.textContent
设置为字符串
“selectedComment”
,而不是值。这就是为什么它可以在日志中正常打印(因为您正在记录值)。将该行更改为:

currentComment.textContent = selectedComment;

(请注意,没有引号,这将创建一个文本值为
selectedComment
的字符串)

尝试使用value而不是textContent,来写入textarea的内容


我不知道所有的浏览器,但在其中一些浏览器中,文本内容是只读的。

谢谢@Josh,我修改了代码,但没有用,我在这里询问之前已经使用过,两个版本都没有修改文本。还有其他想法吗?对我来说,这似乎像预期的那样有效:这基本上就是你想要发生的事情吗?一旦你从文本框中删除文本并选择一个元素,它就不会添加任何内容。似乎是这样。值正在运行。谢谢你们两位的快速回复