Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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,我想使用javascript创建包含1000个单词的虚拟文本。我的想法是使用这样的数组 var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from va

我想使用javascript创建包含1000个单词的虚拟文本。我的想法是使用这样的数组

var words =["The sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", ".", "All", "this happened", "more or less","." ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story","." ,"It", "was", "a pleasure", "to", "burn"];

然后使用javascript随机输出1000个单词。我将如何使用javascript实现这一点?还有其他方法吗

这将以随机顺序记录所有单词:

函数洗牌(数组){
var currentIndex=array.length,temporaryValue,randomIndex;
//虽然还有一些元素需要洗牌。。。
而(0!==currentIndex){
//选择剩余的元素。。。
randomIndex=Math.floor(Math.random()*currentIndex);
currentIndex-=1;
//并将其与当前元素交换。
临时值=数组[currentIndex];
数组[currentIndex]=数组[randomIndex];
数组[randomIndex]=临时值;
}
返回数组;
}
var words=[“天空”,“上面”,“港口”,“是”,“电视的颜色”,“调谐”,“一个死频道”,“所有”,“这发生了”,“或多或少”,“我”,“有”,“故事”,“一点一点”,“来自不同的人”,“和”,“一般”,“发生”,“在这种情况下”,“每一次”,“它”,“是”,“一个不同的故事”,“它”,“是”,“一种乐趣”,“to”,“burn”];
洗牌(单词);
var语句=单词。连接(“”)
//演示
控制台日志(文字);
控制台日志(句子);
文档。写(句子)
看看这个

var words=[“天空”,“上面”,“港口”,“是”,“电视的颜色”,“调谐”,“到”,“一个死频道”,“所有”,“这发生了”,“或多或少”,“我”,“有”,“这个故事”,“一点一点”,“来自不同的人”,“和”,“一般”,“发生了”,“在这种情况下”,“每一次”,“它”,“是”,“一个不同的故事”,“它”,“是”,“很荣幸”、“去”、“烧”];
var text=[];
var x=1000;
而(--x)text.push(单词[Math.floor(Math.random()*words.length)];
document.write(text.join(“”)
var m=words.length,
t、 i,结果=[];
而(m&&result.length<100){
i=Math.floor(Math.random()*m--);
t=arr[m];
arr[m]=arr[i];
arr[i]=t;
结果推送(arr[m]);
}

因为我个人喜欢Lorem Ipsum,那么这个呢

var r = new XMLHttpRequest();
r.onload = function() {
    if (r.readyState != 4 || r.status != 200) return;   // bad query or something...
    document.getElementById('LIpsum').textContent = r.responseText;
};
r.open('GET', 'https://baconipsum.com/api/?type=meat-and-filler&paras=18&format=text');
r.send();
Bacon Ipsum上的文档(我所知道的唯一允许CORS请求的Ipsum):

var words=[“天空”,“上面”,“港口”,“是”,“电视的颜色”,“调谐”,“到”,“一个死频道”,“所有”,“这发生了”,“或多或少”,“我”,“有”,“故事”,“一点一点”,“来自不同的人”,“和”,“一般”,“发生了”,“在这种情况下”,“每一次”,“它”,“是”,“一个不同的故事”,“它”,“是”很高兴“,”去“,”烧“];
函数随机化语句(){
var n=1000;
var句子=”;
而(n--){
句子+=单词[Math.floor(Math.random()*words.length)]+“”;
}
返回判决;
}

document.writeln(randomstation())
我会将标点符号和大写字母分开,然后计算单词,而不是数组中使用的项目。下面是一个JSFIDLE:

代码如下:

var words =["the sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", "all", "this happened", "more or less" ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story", "it", "was", "a pleasure", "to", "burn"],
    punctuation = [".", ","],
    text = "",
    phrase,
    punc,
    count = 0,
    nextCapital = true;
while(count<1000) {
  phrase = words[Math.floor(Math.random() * words.length)]
  text += nextCapital ? phrase[0].toUpperCase() + phrase.slice(1): phrase;
  nextCapital = false;
  if ( Math.random() > .8 ) {
    punc = punctuation[Math.floor(Math.random() * punctuation.length)];
    if ( punc === "." ) nextCapital = true;
    text += punc;
  }
  text += " ";
  count = text.match(/\S+/g).length;
}
document.write(text);
var words=[“天空”,“上面”,“港口”,“是”,“电视的颜色”,“调谐”,“到”,“一个死频道”,“所有”,“这发生了”,“或多或少”,“我”,“有”,“故事”,“一点一点”,“来自不同的人”,“和”,“一般”,“发生”,“在这种情况下”,“每一次”,“它”,“是”,“一个不同的故事”,“它”,“是”,“一种乐趣”,“到”烧录“],
标点符号=[“,”,“],
text=“”,
短语,
朋克,
计数=0,
nextCapital=true;
而(计数8){
punc=标点符号[Math.floor(Math.random()*标点符号.length)];
如果(punc==”)nextCapital=true;
text+=punc;
}
文本+=”;
count=text.match(/\S+/g).length;
}
文件。书写(文本);

for(var i=1000;i--;)document.body.innerHTML+=words[Math.floor(Math.random()*words.length)];我认为应该在单词之间添加空格。很好。因此,您的答案将提供一个“句子”“这些词多次出现。而我的答案将使用每一个单词一次。答案已完成我跟踪了你,但它犯了一个错误,所以它不能很好地工作,我无法找出它。我有另一个建议删除循环,但我不知道怎么做。
var words =["the sky", "above", "the port","was", "the color of television", "tuned", "to", "a dead channel", "all", "this happened", "more or less" ,"I", "had", "the story", "bit by bit", "from various people", "and", "as generally", "happens", "in such cases", "each time", "it", "was", "a different story", "it", "was", "a pleasure", "to", "burn"],
    punctuation = [".", ","],
    text = "",
    phrase,
    punc,
    count = 0,
    nextCapital = true;
while(count<1000) {
  phrase = words[Math.floor(Math.random() * words.length)]
  text += nextCapital ? phrase[0].toUpperCase() + phrase.slice(1): phrase;
  nextCapital = false;
  if ( Math.random() > .8 ) {
    punc = punctuation[Math.floor(Math.random() * punctuation.length)];
    if ( punc === "." ) nextCapital = true;
    text += punc;
  }
  text += " ";
  count = text.match(/\S+/g).length;
}
document.write(text);