将生成的<p>标记值的数据传递给twitter widget text-data属性

时间:2015-12-22 00:23:14

标签: javascript arrays twitter attributes widget

我正在尝试在新推文中将数据从p标记传递到data-text属性。 问题是,标签内显示的数据是从数组生成的,而我的twitter小部件无法识别动态值。

这是JS Fiddle

<!--   <div id="DIV1">This is the first quote</div>   -->
<p id="DIV1"></p>
<script>
    var arr = ["This is first quote", 
              "This is second quote", 
              "This is third quote", 
              "This is fourth quote", 
              "This is fifth quote"];

    function choseRandQuote() {
        var  randomQuote = Math.floor(Math.random()*(arr.length)); 
        document.getElementById('DIV1').innerHTML = arr[randomQuote];
    } 

</script>

<div class="button-place"> <!-- /reserved exclusevly for button-->
    <button id="btn-gnr" class="btn" onclick="choseRandQuote();">Generate!</button>
</div><!-- /end .button -->
<br />
<br />
<div id="twtbtn"></div>
<!-- and-->
<script>
    var MyDiv1 = document.getElementById('DIV1').innerHTML;
    window.twttr = (function(d, s, id) {
        var t, js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;

        js = d.createElement(s);
        js.id = id;
        js.src = "https://platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js, fjs);

        return window.twttr || (t = {
            _e: [],
            ready: function(f) {
                t._e.push(f)
            }
        });
    }(document, "script", "twitter-wjs"));

    twttr.ready(function(twttr) {
        twttr.widgets.createShareButton(
            'http://twitter.com',
            document.getElementById('twtbtn'),
            function(el) {
                console.log("Button created.")
            }, 
            {
                count: 'none',
                text: "Quote: " + MyDiv1,
                via: 'alex_ryzhuk',
                hashtags: 'FreeCodeCamp'
            }
        );

        twttr.events.bind('tweet', function(event) {
            console.log(event, event.target);
        });
    });
</script>

2 个答案:

答案 0 :(得分:0)

您的推文按钮在dom加载时呈现,因此您的div为空。每次更改报价时都需要重新生成推文按钮。

<!--   <div id="DIV1">This is the first quote</div>   -->
<p id="DIV1"></p>
  <script>
var arr = ["This is first quote", 
			  "This is second quote", 
			  "This is third quote", 
			  "This is fourth quote", 
			  "This is fifth quote"];
 
function choseRandQuote() {
	var  randomQuote = Math.floor(Math.random()*(arr.length)); 
	document.getElementById('DIV1').innerHTML = arr[randomQuote];
   addTwitter();
	} 
        </script>
<div class="button-place"> <!-- /reserved exclusevly for button-->
                <button id="btn-gnr"
                        class="btn"
                        onclick="choseRandQuote();"> 
                    Generate! 
                </button>
                </div><!-- /end .button -->
<br />
<br />
<div id="twtbtn"></div>

<script>
  
function addTwitter(){
var MyDiv1 = document.getElementById('DIV1').innerHTML;
window.twttr = (function(d, s, id) {
  var t, js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);
  return window.twttr || (t = {
    _e: [],
    ready: function(f) {
      t._e.push(f)
    }
  });
}(document, "script", "twitter-wjs"));
twttr.ready(function(twttr) {
  twttr.widgets.createShareButton(
    'http://twitter.com',
    document.getElementById('twtbtn'),
    function(el) {
      console.log("Button created.")
    }, {
      count: 'none',
      text: "Quote: " + MyDiv1,
      via: 'alex_ryzhuk',
      hashtags: 'FreeCodeCamp'
    }
  );
  twttr.events.bind('tweet', function(event) {
    console.log(event, event.target);
  });
});
}
addTwitter();
</script>

答案 1 :(得分:0)

Jquery方法:

$(".btn-tweet").click(function () {
        var width  = 575,
        height = 400,
        left   = ($(window).width()  - width)  / 2,
        top    = ($(window).height() - height) / 2,
        url    = 'http://twitter.com/share?text=' + $info.quote + " - " + $info.author,
        opts   = 'status=1' +
                 ',width='  + width  +
                 ',height=' + height +
                 ',top='    + top    +
                 ',left='   + left;

        window.open(url, 'twitter', opts);
    });

Example