2017-02-02 5 views
0

무작위 따옴표 생성기 인 codePen입니다. 생성 된 견적을 트윗해야하는 트윗 버튼이 있습니다. 현재 그것을 누르면, 표시되는 현재 인용 부호가 아니라 다른 임의의 인용 부호를 트윗합니다.버튼이 제대로 트윗되지 않음

$(".twitter-share-button").click(function(){ 
    $(this).attr("href", 'https://twitter.com/intent/tweet?text=' + randomNumber); 
    }); 

function tweetIt(){ 
var randomNumber = Math.floor(Math.random() * (quotes.length)); 
window.open("https://twitter.com/intent/tweet?text=" + quotes[randomNumber]); 
} 

나는 견적에 있지만 아무 소용되는 현재의 ID를 선택하여이 문제를 해결 시도 :이와

function tweetIt(){ 
    var tweet = document.getElementById('quote').value; 
    window.open("https://twitter.com/intent/tweet?text=" + quotes[randomNumber]); 
}; 

어떤 도움도 대단히 감사하겠습니다 여기에 기능입니다. 현재 견적을 트윗 할 경우

function newQuote() { 
    var randomNumber = Math.floor(Math.random() * (quotes.length)); 
    // The content for your tweet is being set here 
    document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber]; 
}; 

그래서 당신이 특정 요소에 대한 textContent 속성을 대상으로해야합니다 :

답변

0

당신은 "quoteDisplay"의 id를 가진 요소 내에 현재 견적을 저장하는 :

function tweetIt(){ 
    // Get the contents of your current tweet 
    var tweet = document.getElementById('quoteDisplay').textContent; 
    // Open a new window with that tweet content 
    window.open("https://twitter.com/intent/tweet?text=" + tweet); 
}; 
+0

'innerText'를 사용하지 마십시오. 표준이 아니며 FireFox에는 존재하지 않습니다. 'textContent'를 사용하십시오. – Barmar

+0

명확하게 설명해 주셔서 감사합니다. 또한 감사합니다. @Barmar – YoungCoder

+0

@Barmar 그 점에 대해 감사드립니다. 나는 해답도 조정했습니다. –

관련 문제