javascript - 寻找随机选择、连接存储在变量中的文本字符串

标签 javascript jquery string random concatenation

问题

在我的 script.js 中,变量 var fullURL =没有在 teaser1 中发布实际的文本, teaser2teaser3我已存储在变量中。我基本上希望当人们点击fa-twitter时随机选择三个预告片之一

脚本.js

function shareTeam(){
    $(".fa-twitter").click(function(){

         // Grabs the names of all the players in the span
         // Sets a variable marking the indexOf each of the names
         // If the index doesn't find a space, it returns -1, which returns the full name
         // Otherwise it will return only what follows the space
         var lastNames = $("li span").map(function() {
           var name = $(this).text();
           var index = name.indexOf(" ");
           return index == -1 ? name : name.substring(index + 1);
         }).get();
         console.log(lastNames);

        var regularNames = lastNames.slice(0, 3); // Same as below, but no shuffling

        regularName1 = regularNames[0]; // Forward
        regularName2 = regularNames[1]; // Forward
        regularName3 = regularNames[2]; // Defenseman

        // Find me a random number between 1 and 3
        // Where 1 is the start number and 3 is the number of possible results
        var teaser = "teaser";
        var rand = Math.floor(Math.random() * 3) + 1;
        console.log(rand);

        // Concatenate the two strings together
        teaseRand = teaser.concat(rand);

        // These are the components that make up that fullURL
        var baseURI = "https://twitter.com/intent/tweet?url=";
        var twitterUsername = "stltoday";
        var interactiveURL = "http://graphics.########.com/STLblues";

        // Randomly generate one of three teasers
        var teaser1 = regularName3 + " to " + regularName2 + " back to " + regularName1 + " — GOAL! Create your own all-team #STLBlues team: ";
        var teaser2 = "I picked my #STLBlues dream team. See which players I've chosen and build your own: ";
        var teaser3 = "My #STLBlues team will skate circles around yours! Pick your team: ";

        // This is the full url that will be switched in and out
        // var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;
        var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;

        // It needs to be encoded properly as well
        var encodedURL = encodeURIComponent(fullURL)
        console.log(fullURL);
        console.log(encodedURL);



        // Change the href to the link every time the Twitter button is clicked
        var changeLink = $("link--twitter").attr("href", encodedURL);

        // if (lastNames.length === 6) {

        // } else {
        //     // Generic teaser
        //     var teaser4 = "Pick your #STLBlues dream team from 50 of the best @StLouisBlues to hit the ice: " + interactiveURL + " (via @stltoday)";
        // }
    });
}

最佳答案

不幸的是,这个 teaserRand 值将是“teaser1”或“teaser2”或“teaser3”,而不是变量 teaser1 或 teaser2 或 teaser3 的值(如果有意义的话)。根据您的要求,您需要将预告片添加到数组中,然后从中随机访问。例如如果数组称为 teaser 那么您将需要执行 teaser[rand] ,显然您将需要计算从 0 到 2 而不是 1 到 3 的 rand,就像您现在所做的那样。

请检查我在此处创建的代码笔

http://codepen.io/19sthil80/pen/VKPqkR?editors=1111

$(document).ready(function(){
var teasers = [];
     // Grabs the names of all the players in the span
     // Sets a variable marking the indexOf each of the names
     // If the index doesn't find a space, it returns -1, which returns the full name
     // Otherwise it will return only what follows the space
     var lastNames = $("li span").map(function() {
       var name = $(this).text();
       var index = name.indexOf(" ");
       return index == -1 ? name : name.substring(index + 1);
     }).get();
     console.log(lastNames);

    var regularNames = lastNames.slice(0, 3); // Same as below, but no shuffling

    regularName1 = regularNames[0]; // Forward
    regularName2 = regularNames[1]; // Forward
    regularName3 = regularNames[2]; // Defenseman

    // Find me a random number between 1 and 3
    // Where 1 is the start number and 3 is the number of possible results
    var teaser = "teaser";
    var rand = Math.floor(Math.random() * 3);
    console.log(rand);

    // Concatenate the two strings together
    teaseRand = teaser.concat(rand);

    // These are the components that make up that fullURL
    var baseURI = "https://twitter.com/intent/tweet?url=";
    var twitterUsername = "stltoday";
    var interactiveURL = "http://graphics.########.com/STLblues";

    // Randomly generate one of three teasers
    var teaser1 = regularName3 + " to " + regularName2 + " back to " + regularName1 + " — GOAL! Create your own all-team #STLBlues team: ";
    var teaser2 = "I picked my #STLBlues dream team. See which players I've chosen and build your own: ";
    var teaser3 = "My #STLBlues team will skate circles around yours! Pick your team: ";
teasers.push(teaser1);teasers.push(teaser2);teasers.push(teaser3);
    // This is the full url that will be switched in and out
    // var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teaseRand;
    var fullURL = baseURI+interactiveURL+"&via="+twitterUsername+"&text="+teasers[rand];

    // It needs to be encoded properly as well
    var encodedURL = encodeURIComponent(fullURL)
    console.log(fullURL);
    console.log(encodedURL);



    // Change the href to the link every time the Twitter button is clicked
    var changeLink = $("link--twitter").attr("href", encodedURL);

    // if (lastNames.length === 6) {

    // } else {
    //     // Generic teaser
    //     var teaser4 = "Pick your #STLBlues dream team from 50 of the best @StLouisBlues to hit the ice: " + interactiveURL + " (via @stltoday)";
    // }
});

关于javascript - 寻找随机选择、连接存储在变量中的文本字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650309/

相关文章:

javascript - Knockout.js 3.4.1 抛出多重绑定(bind)错误

javascript - URL.createObjectURL 和字符集 UTF-8

javascript - 在尝试任何进一步处理之前检查用户是否存在于 Firestore 中

javascript - 使用 jQuery 的模态窗口

javascript - Jquery DataTables 默认按日期排序列

python - 如何处理这个异常 'ascii' codec can't decode?

javascript - 无法使用 $(this) 定位元素

javascript - jquery .ajax() 问题

string - LUA:在双引号中查找并返回字符串

java - 如何计算字体的宽度?