简体   繁体   English

jQuery .attr只工作一次

[英]jQuery .attr only works once

I'm creating a simple quote machine with a tweet button that tweets the quote. 我正在创建一个带有推文按钮的简单引号机,该推文按钮会发送推文。 When the page loads I simulate the "New Quote" button being pushed so the tweet button's url is updated with the quote, but after that the tweet button won't update with the new quote after the "New Quote" button is pushed. 当页面加载时,我模拟按下“ New Quote”按钮,以便用引号更新tweet按钮的url,但是在此之后,在按下“ New Quote”按钮后,tweet按钮将不使用新引号进行更新。

HTML 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 使用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");

)};

The variable quotes being an array of strings with the quote and the author separated by a / 变量引号是一个字符串数组,引号和作者之间用/分隔

One possible problem is that index is global scoped because of the lack of "var index ..." 一个可能的问题是,由于缺少“ var index ...”,索引处于全局范围内

also you should use .click(function(){...}); 您也应该使用.click(function(){...}); to bind onclick events to dom. 将onclick事件绑定到dom。

Oddly enough at the very bottom of your javascript, these two characters are reversed. 奇怪的是,在JavaScript的最底部,这两个字符相反。 Maybe that's it? 也许就是这样吗?

)};

Should be 应该

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM