jQuery .attr只能运行一次

时间:2016-02-09 15:29:42

标签: javascript jquery twitter

我创建了一个带有推文按钮的简单报价机,可以发布报价。页面加载时,我会模拟"新报价"按钮被按下,所以推文按钮的网址会被引用更新,但在此之后,推文按钮在"新报价"之后不会使用新报价进行更新。按钮被按下。

HTML

<h1>Random Quote Generator</h1>

<p>These are some random quotes I found online by programmers. Enjoy!</p>

<button id="newQuote">New Quote</button>

<div id="quote"><span id="saying"></span><br/><i id="author"></i></div>

<a class="twitter-share-button" data-size="large" data-count="none">Tweet</a>

的Javascript

$(document).ready(function() {

window.twttr = (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
    if (d.getElementById(id)) return t;
    js = d.createElement(s);
    js.id = id;
    js.src = "https://platform.twitter.com/widgets.js";
    fjs.parentNode.insertBefore(js, fjs);
    t._e = [];
    t.ready = function(f) {
        t._e.push(f);
    };
    return t;
}(document, "script", "twitter-wjs"));

quotes= ["“There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies. The first method is far more difficult.”/- C.A.R. Hoare", "“If debugging is the process of removing software bugs, then programming must be the process of putting them in.”/- Edsger Dijkstra", "“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.”/- Bill Gates", "“Nine people can’t make a baby in a month.” (regarding the addition of more programmers to get a project completed faster)/– Fred Brooks", "“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”/– Brian W. Kernighan", "“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.”/– Martin Golding", "“C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg.”/– Bjarne Stroustrup", "“When debugging, novices insert corrective code; experts remove defective code.”/– Richard Pattis", "“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”/– Eric S. Raymond", "“Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program.”/– Linus Torvalds"];

$("#newQuote").on("click", function() {
    index = Math.floor(Math.random() * ((quotes.length-1) + 1));
    quote = quotes[index].split("/");
    $("#saying").text(quote[0]);
    $("#author").text(quote[1]);
    $('.twitter-share-button').attr("href", "https://twitter.com/intent/tweet?text=" + quote[0] + " " + quote[1]);
});
$("#newQuote").trigger("click");

)};

变量引号是一个字符串数组,引号和作者用/

分隔

2 个答案:

答案 0 :(得分:0)

一个可能的问题是索引是全局范围的,因为缺少&#34; var索引......&#34;

你也应该使用.click(function(){...});将onclick事件绑定到dom。

答案 1 :(得分:0)

奇怪的是,在你的javascript底部,这两个字符是相反的。也许那就是它?

)};

应该是

});