0

所以这是我的脚本:

function getRandomArrayIndex(source_array) {

return Math.floor(Math.random() * source_array.length);

}

function getRandomArrayEntry(source_array) {

var random_index = getRandomArrayIndex(source_array);

return source_array[random_index];

}

function getRandomV5test() {

var v5tests = [
["This should tell me I have the following amount of minutes: "],
["This is the number of times I want to jump up and down: "],
["This is a quote."],
]; var random_v5test = getRandomArrayEntry(v5tests);

return random_v5test;

}

function displayV5test(v5test) {
    var element = document.getElementById("v5test");
    if(element !== null){
        var TEXT_ROW = 0;
        var LINK_ROW = 1;
        var v5test_text = v5test[TEXT_ROW];
        var v5test_link = v5test[LINK_ROW]; 
        if (v5test_link != null) {
            element.innerHTML = '<a href="' + v5test_link + '">' + v5test_text + '</a>';
        } else {
            element.innerHTML = v5test_text + '<span id="time"></span>';
        }
    }
}

function generateRandomV5test(){

var random_v5test = getRandomV5test();

displayV5test(random_v5test);

}

它的作用是结合多个同级脚本,生成一个带有随机结果的短语。它在其他脚本之前运行,这些脚本将它们的随机短语插入指定的跨度 id ('time'),页面上的最终结果可能类似于以下内容:

这应该告诉我我有以下分钟数:40

问题是这给了我任何灵活性——我在“v5test_text”之后指定的跨度必须总是在之后。此外,无论脚本选择了哪个数组选项,第三个数组选项仍将返回一个随机数,如下所示:

这是一个报价。25

我想要做的是将 html 代码放在不同的数组选项中,如下所示:

var v5tests = [
["This should tell me I have <span id="time"></span> minutes. "],
["I want to jump up and down <span id="time"></span> times. "],
["This is a quote."],

所以,这真的是一个简单的问题。如果我想防止在此实例中使用的 html 代码被脚本本身混淆,我应该使用什么语法?知道这一点将使我在网站内容方面具有几乎无限的灵活性。

任何帮助将不胜感激!:)

4

2 回答 2

0

看起来您需要使用反斜杠转义字符转义引号。

var v5tests = [
    "This should tell me I have <span id=\"time\"></span> minutes.",
    "I want to jump up and down <span id=\"time\"></span> times. ",
    "This is a quote."
];
于 2012-08-14T18:56:00.320 回答
0

使用单引号来定义你的字符串...

var v5tests = [
['This should tell me I have <span id="time"></span> minutes.'],
['I want to jump up and down <span id="time"></span> times.'],
['This is a quote.'],

或在字符串中转义双引号:

var v5tests = [
["This should tell me I have <span id=\"time\"></span> minutes. "],
["I want to jump up and down <span id=\"time\"></span> times. "],
["This is a quote."],
于 2012-08-14T18:53:44.923 回答