2017-09-03 119 views
0

当我点击按钮时,使用jquery更改div子元素的属性时出现问题。 正确的场景:当我按下一个python时,twitter按钮的属性根据ii的新值而改变。单击按钮时更改属性Jquery

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) {}); 
 
}
<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>

+0

也counterabutors可能意味着是反对者 –

回答

0

你需要tweek你的代码是:

$('.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代码中。

+0

感谢您的回应,但我发现加载twitter按钮中的错误。 –