我可以在href的末尾添加一个字符串吗?

时间:2016-08-27 22:48:52

标签: javascript html arrays twitter-bootstrap

我正在为FreeCodeCamp制作一个随机引用生成器我需要帮助才能使推文按钮正常工作。我的引号作为字符串存储在一个数组中。是否可以将它们添加到我的href结尾:href =' http://twitter.com/home/?status=string

http://codepen.io/tyl-er/pen/KrjWVA?editors=1000

<head>
    <script src="https://use.fontawesome.com/41c0e08ce5.js"></script>
</head>

<div class="flip">
    <div onclick="randomIndex" class="card">
        <div class="face front">
            <!--logo here-->
        </div>
        <div class="face back">

            <textarea rows="10" cols="30" type=text id="mytext"></textarea>
        </div>
    </div>
</div>

<div class="div10">
    <p>(Spoliers maybe ¯\_(ツ)_/¯) <del>Written</del> and coded by Tyler Pelzer</p>
    <a class="btn btn-social-icon btn-twitter" target="blank" href='http://twitter.com/home/?status=Twitter is like the lunch meeting with potential clients before you do the pitch. via @blogtyrant'>
        <span class="fa fa-twitter"></span>
    </a>
</div>


$('.flip').click(function printquote(){

var strings = []; // <--superlong array of quotes
var randomIndex = Math.floor(Math.random() * strings.length);
var randomString = strings[randomIndex];
document.getElementById("mytext").value = randomString;
//that was easy^

});

$('.flip').click(function(){
        $(this).find('.card').addClass('flipped').mouseleave(function()
        {
            $(this).removeClass('flipped');

        });
        return false;
});

1 个答案:

答案 0 :(得分:0)

您可以设置href属性,就像设置textarea的值一样。在链接标记上添加ID:

ctags -R

编辑:正如您希望单独对翻转点击处理程序执行此操作,让我们重构以下代码:

document.getElementById('twitterLink').href = 'https://twitter.com/home?status=' + encodeURIComponent(randomString);

进入这个:

$('.flip').click(function printquote(){
    var strings = []; // <--superlong array of quotes
    var randomIndex = Math.floor(Math.random() * strings.length); 
    var randomString = strings[randomIndex];
    document.getElementById("mytext").value = randomString;
    //that was easy^
});

通过这种方式,您可以使用此新代码共享功能以获取随机引用:

function getQuotes() {
    return []; // <--superlong array of quotes
}

function getRandomEntry(entries) {
    return entries[Math.floor(Math.random() * entries.length)];
}

$('.flip').click(function printquote(){
    document.getElementById("mytext").value = getRandomEntry(getQuotes());
    //that was easy^
});