简体   繁体   English

带有JavaScript测验的Twitter分享按钮

[英]Twitter share button with JavaScript Quiz

I've got a JavaScript Quiz which produces a result saying the score. 我有一个JavaScript测验,它会产生一个说分数的结果。

test.innerHTML = "<h2>You got "+correct+" of "+questions.length+" questions correct</h2>";
    _("test_status").innerHTML = "Test Completed";

I would like to have a Twitter share button which allows the user to tweet the score that the user has got from the quiz. 我希望有一个Twitter共享按钮,该按钮允许用户发布用户从测验中获得的分数。

Any ideas how to do this? 任何想法如何做到这一点?

You can: 您可以:

  1. Use this URL however you like: "https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions.length+" questions on JavaScript Quiz!" 使用此URL,但是您喜欢: "https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions.length+" questions on JavaScript Quiz!" (although you will need JavaScript to create it). (尽管您将需要JavaScript来创建它)。
  2. Use a simple <a> link, like this: <a class="twitter-share-button" href="#" id="twitter-button">Tweet</a> and add document.getElementById("twitter-button").setAttribute("href", "https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions+" questions on JavaScript Quiz!"); 使用简单的<a>链接,如下所示: <a class="twitter-share-button" href="#" id="twitter-button">Tweet</a>并添加document.getElementById("twitter-button").setAttribute("href", "https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions+" questions on JavaScript Quiz!"); to your script. 到您的脚本。
  3. Add the above link, plus this code to your script, to make a fancy button: !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs'); 将上面的链接以及此代码添加到您的脚本中,以创建一个精美的按钮: !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs'); .

Check out this snippet (the button doesn't seem to work in the snippet, but it should work locally and on your website): 查看以下代码段(该按钮在该代码段中似​​乎不起作用,但应该在本地和您的网站上都可以使用):

 <a class="twitter-share-button" href="#" id="twitter-button">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs'); correct = 5; questions = 10; document.getElementById("twitter-button").setAttribute("href", "https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions+" questions on JavaScript Quiz!");</script> 

You can generate all the button script here . 您可以在此处生成所有按钮脚本。 Also, have a look here . 另外, 在这里看看。

Hope this helps! 希望这可以帮助! :) :)

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

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