【问题标题】:Looking to randomly select, concatenate string of text stored in variable希望随机选择,连接存储在变量中的文本字符串
【发布时间】:2017-01-31 16:43:04
【问题描述】:

问题

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

scripts.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)";
        // }
    });
}

【问题讨论】:

  • 你永远不想用encodeURIComponent编码整个URL,只需要查询字符串
  • 您介意发布代码 sn-p 吗?

标签: javascript jquery string random concatenation


【解决方案1】:

不幸的是,如果有意义的话,这个 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)";
    // }
});

【讨论】:

  • 我在代码笔中添加了一个代码,它可以按照您想要的方式工作。如果我的回答有帮助,请标记它已回答。谢谢
  • 我想弄清楚你改变了什么?
  • 我创建了一个名为 teasers 的数组,我已经将你的 teasers 推入其中,我正在从该数组中随机选择 teasers...
猜你喜欢
  • 1970-01-01
  • 2020-06-07
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
相关资源
最近更新 更多