0

我在尝试使用 JavaScript 创建动态 Twitter 按钮时遇到问题。我试图让 twitter 按钮来推特我正在编程的计算器的输出。问题是按钮不会显示。我究竟做错了什么?请帮忙。

var twit_link = document.createElement('a');
  twit_link.setAttribute('href', 'https://www.twitter.com/share');
  twit_link.setAttribute('class', 'twitter-share-button');
  twit_link.setAttribute('url', 'http://www.twitter.com/obamamakes';
  twit_link.setAttribute('data-count', 'none');
  twit_link.setAttribute('data-via', 'ObamaMakes');
  twit_link.setAttribute('data-text', 'In the time it takes me to ' + activity + ', Barack Obama makes $' + i.toFixed(1);
  twit_link.innerHTML = "Tweet";

  $("#CalcOutput").html("In the time it takes me to <span class=\"num\">" + activity + "</span>,<br />Barack Obama makes $<span class=\"num\">" + i.toFixed(1) + "</span>");

  $("#CalcOutput").append(twit_link);

我做了大家建议的更改,但是计算器应该产生的整个输出仍然没有与 twitter 按钮一起显示。我也应该提供计算器代码吗?

4

3 回答 3

0

使用 jQuery的jsFiddle(仅限):

$(document).ready(function(){
//TEst Data - Starts
    var activity = 'Test';
    var i = 12;
//Test Data Ends
var twit_link = $('<a />');
twit_link.attr('href', 'https://www.twitter.com/share');
twit_link.addClass('twitter-share-button');
twit_link.attr('url', 'http://www.twitter.com/obamamakes');
twit_link.attr('data-count', 'none');
twit_link.attr('data-via', 'ObamaMakes');
twit_link.attr('data-text', 'In the time it takes me to ' + activity + ', Barack Obama makes $' + i.toFixed(1));
    twit_link.html('Tweet');

$("#CalcOutput").html("In the time it takes me to <span class=\"num\">" + activity + "</span>,<br />Barack Obama makes $<span class=\"num\">" + i.toFixed(1) + "</span>");
$("#CalcOutput").append(twit_link);

});
于 2012-10-16T18:15:56.220 回答
0

你的语法不正确。您缺少一些结束括号:

var twit_link = document.createElement('a');
  twit_link.setAttribute('href', 'https://www.twitter.com/share');
  twit_link.setAttribute('class', 'twitter-share-button');
  twit_link.setAttribute('url', 'http://www.twitter.com/obamamakes'); // missing ) here
  twit_link.setAttribute('data-count', 'none');
  twit_link.setAttribute('data-via', 'ObamaMakes');
  twit_link.setAttribute('data-text', 'In the time it takes me to ' + activity + ', Barack Obama makes $' + i.toFixed(1)); // missing ) here
  twit_link.innerHTML = "Tweet";

  $("#CalcOutput").html("In the time it takes me to <span class=\"num\">" + activity + "</span>,<br />Barack Obama makes $<span class=\"num\">" + i.toFixed(1) + "</span>");

  $("#CalcOutput").append(twit_link);

您可以通过浏览器的开发人员工具进行检查。它将在控制台中显示错误。

正如 Abdullah 提到的,无论如何你都可以使用 jQuery 来完成所有这些工作:

var twit_link = $('<a/>', {
    href: 'https://www.twitter.com/share',
    'class': 'twitter-share-button',
    url: 'http://www.twitter.com/obamamakes',
    'data-count': 'none',
    'data-via': 'ObamaMakes',
    'data-text': ... // whatever you want here
    }).text('Tweet');
$("#CalcOutput").html("In the time it takes me to <span class=\"num\">" + activity + "</span>,<br />Barack Obama makes $<span class=\"num\">" + i.toFixed(1) + "</span>");
$("#CalcOutput").append(twit_link);

jsFiddle 示例

于 2012-10-16T18:14:31.440 回答
0

你有几个语法错误。如果你仍然使用 jQuery,你可以清理它:

var twit_link = $('<a>Tweet</a>');
twit_link.attr('href', 'https://www.twitter.com/share');
twit_link.attr('class', 'twitter-share-button');
twit_link.attr('url', 'http://www.twitter.com/obamamakes');
twit_link.data('count', 'none');
twit_link.data('via', 'ObamaMakes');
twit_link.data('text', 'In the time it takes me to ' + activity + ', Barack Obama makes $' + i.toFixed(1));

$("#CalcOutput").html("In the time it takes me to <span class=\"num\">" + activity + "</span>,<br />Barack Obama makes $<span class=\"num\">" + i.toFixed(1) + "</span>");
$("#CalcOutput").append(twit_link);
于 2012-10-16T18:10:24.737 回答