将长短语拆分为数组

时间:2014-04-29 19:40:43

标签: javascript html arrays button string-split

我需要使用短语

这是一年中你清理壁橱,把货架上的灰尘和地板修整起来的时候。一旦你处理了灰尘和污垢,那么一些数字清洁呢?浏览所有文件和计算机似乎是一项艰巨的任务,但我们找到了使这个过程相当轻松的方法。

并按下按钮

将其拆分为数组

在每一步迭代该数组

随身携带构建SPAN元素以及属性

将SPAN元素添加到原始DIV

向SPAN元素或DIV添加单击处理程序,这会导致SPAN上的样式在鼠标悬停时更改。

到目前为止我已经

function splitString(stringToSplit, separator) {
  var arrayOfStrings = stringToSplit.split(separator);

  print('The original string is: "' + stringToSplit + '"');
  print('The separator is: "' + separator + '"');
  print("The array has " + arrayOfStrings.length + " elements: ");

  for (var i=0; i < arrayOfStrings.length; i++)
    print(arrayOfStrings[i] + " / ");
}

var space = " ";
var comma = ",";

splitString(tempestString, space);
splitString(tempestString);
splitString(monthString, comma);
for (var i=0; i < myArray.length; i++)
{

}
var yourSpan = document.createElement('span');
yourSpan.innerHTML = "Hello";

var yourDiv = document.getElementById('divId');
yourDiv.appendChild(yourSpan);

yourSpan.onmouseover = function () {
    alert("On MouseOver");

}

和html我有

The DIV that will serve as your input (and output) is here, with
        id="transcriptText":</p>
      <div id="transcriptText"> It’s that time of year when you clean out your
        closets, dust off shelves, and spruce up your floors. Once you’ve taken
        care of the dust and dirt, what about some digital cleaning? Going
        through all your files and computers may seem like a daunting task, but
        we found ways to make the process fairly painless.</div>
      <br>
      <div id="divideTranscript" class="button">&nbsp;Transform the
        Transcript!&nbsp; </div>

有关如何搬家的任何帮助?我已经被困了很长时间

1 个答案:

答案 0 :(得分:0)

嗯,首先,这看起来像家庭作业。

那就是说,我会尽力帮助你而不给你实际的代码,因为我们不应该给家庭作业提供实际工作的解决方案。您将字符串拆分太多次(根据您给出的指示,只需要一次),您必须将拆分调用的结果存储在您的其他代码可以使用的地方。

您的说明是为跨栏添加属性,但不是属性,也不是内容应该是什么。

您的功能应遵循以下说明:

1)拆分字符串。既然没有说明什么,我会假设单词。因此,只将其拆分为空格,并将标点符号保留在原点。

2)使用split()函数返回的单词数组,像你试图的那样迭代它,但是中,用于连接循环的大括号是你想要连接{{ 1}}在原始单词周围开始和结束标记。

3)使用document.createElement()将当前跨度转换为DOM元素。将鼠标悬停并单击处理程序,然后将其追加到div。

将处理程序添加到按钮以调用上述函数。

请注意,使用innerHTML()函数一次插入所有跨度可能更有效,但是您必须再次循环以添加悬停/单击处理程序。