I've a textarea consisting of 30 rows. I want to display the following array elements onto this textarea and add more elements to the textarea. After that, I want to append the added elements to the same array again. The array is given below:

我有一个由30行组成的textarea。我想在这个textarea上显示以下数组元素,并向textarea添加更多元素。之后,我想再次将添加的元素追加到同一个数组中。数组如下:

var words = ['Starbucks', 'Coffee', 'Tea', 'Chai', 'Milk Tea', 'Mocha', 'Drink', 'Smoothy', 'Milk', 'Cappuccino'];

var words = [''Starbucks','Coffee','Tea','Chai','Milk Tea','Mocha','Drink','Smoothy','Milk','Cappuccino'];

How can I accomplish this task? I'm in the learning process of Javascript and I need help with this part for my ongoing research project. If anybody can help me or give me valuable suggestions for accomplishing this, I'd be really grateful.

我怎样才能完成这项任务?我正处于Javascript的学习过程中,我需要帮助我完成正在进行的研究项目。如果有人可以帮助我或者为我提供有价值的建议,我将非常感激。

By the way, I'd tried using Array.join() method but it didn't work for me.

顺便说一下,我尝试使用Array.join()方法,但它对我不起作用。

1 个解决方案

#1


this should work, if i understand what you need ;)

这应该工作,如果我明白你需要的;)

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>words</title>

</head>
<body>

<label for="wordList">words</label>
<textarea id="wordList"></textarea>
<br />
<button onclick="loadFromArray();">Load from array</button>
<button onclick="saveToArray();">Save textarea to array</button>

    <script>
        var words = ['Starbucks', 'Coffee', 'Tea', 'Chai', 'Milk Tea', 'Mocha', 'Drink', 'Smoothy', 'Milk', 'Cappuccino'];
        console.log('initials words = %o', words);

        function loadFromArray() {
            document.getElementById('wordList').value = words.join('\n');
            console.log('loading words %o into textarea', words);
        }

        function saveToArray() {
            words = document.getElementById('wordList').value.split('\n');
            console.log('saving words %o from textarea', words);
        }
    </script>
</body>
</html>

更多相关文章

  1. JS将字符串转换为数组
  2. CSS3(jQUery?)当它悬停时隐藏元素“a”,这样就可以显示元素“b”
  3. ECMAScript6(6):数组的扩展
  4. 希望日期开始结束在一个数组中的while循环中为一个房间ID
  5. 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong
  6. 停止鼠标用javascript双击某些元素
  7. 在量角器中检索子元素的数组
  8. 从特定条件下存储在localStorage中的数组中删除对象?
  9. Python 3.4:试图让这个模块对这个2d数组中的销售总数求和。不工作

随机推荐

  1. 关于PHP中异常错误的处理详细介绍
  2. php解决跨域问题
  3. php数组去重的方法
  4. php购物车的实现原理
  5. PHP相应button中的onclick事件
  6. php主键的作用
  7. php面向对象三大特性
  8. php类和对象的关系
  9. 深入理解PHP与WEB服务器交互
  10. php变量的作用域