单击按钮Jquery时更改属性

时间:2017-09-03 04:57:38

标签: jquery html ecmascript-6

当我更改div子的属性时,我遇到问题,当我点击按钮时使用jquery。 正确的场景:当我按下一个python时,twitter按钮的属性会根据ii的新值发生变化。



var pro = [{
    Name: 'kinto',
    Description: 'A generic JSON document store with sharing and synchronisation capabilities. http://docs.kinto-storage.org/',
    Link: 'https://github.com/Kinto/kinto',
    Language: 'python'
  },
  {
    Name: 'catapult',
    Description: 'Catapult home for performance tools.',
    Link: 'https://github.com/catapult-project/catapult',
    Language: 'python'
  }
];

function next(lang) {
  ii = Math.floor(Math.random() * (pro.length - 0) + 0);
  $(`.name`).html(`${pro[ii].Name}`);
  $(`.${lang}box`).remove(`.twitter${lang}`);
  $(`.${lang}box`).append(`<div class="twitter${lang}">
    <a class="twitter-share-button" href="https://twitter.com/share" data-text="Check this project" data-url="${prolist[ii].Link}" data-hashtags="counteributors" data-via="counteributors">
            Tweet
            </a>
</div>`);
}

//-------------- twitter button
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"));

document.getElementById('shareBtn').onclick = function() {
  FB.ui({
    method: 'share',
    display: 'popup',
    href: 'google.com',
  }, function(response) {});
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" onclick="next('python');">next python</a>
<div class="twitterpython">
  <a id="tweetpython" class="twitter-share-button" href="https://twitter.com/share" data-text="Check" data-url="" data-hashtags="c" data-via="c">
                                            Tweet
                                            </a>

</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您需要将代码调整为:

$('.name').html(pro[ii].Name);
      $('.' + lang + 'box').remove('.twitter' + lang);
      $('.' + lang + 'box').append('<div class="twitter' + lang + '">
        <a class="twitter-share-button" href="https://twitter.com/share" data-text="Check this project" data-url="' + prolist[ii].Link + '" data-hashtags="counteributors" data-via="counteributors">
                Tweet
                </a>
    </div>`);

您需要在javascript代码中正确访问您的javascript变量。您正在使用的语法似乎打算用于实际的html页面,使用html代码。