[英]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: 您可以:
"https://twitter.com/intent/tweet?text=I got "+correct+" correct out of "+questions.length+" questions on JavaScript Quiz!"
"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). <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. !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.