我想从HTML页面中获取一些文字并将其发推出来。
这是我的HTML页面 -
function change() {
quotes = ["Naam toh suna hi hoga", "Mogambo Khush Hua", "Kitne aadmi the?"];
auth = ["Raj", "Mogambo", "Gabbar"];
min = 0;
max = 3;
num = Math.floor(Math.random() * (max - min)) + min;
q = quotes[num]
document.getElementById("quote").innerHTML = quotes[num];
document.getElementById("author").innerHTML = auth[num];
if (num == 0) {
document.getElementById("bod").style.color = 'white';
document.body.style.backgroundColor = 'green';
document.getElementById("another").style.backgroundColor = 'green';
document.getElementById("tweet").style.backgroundColor = 'green';
}
if (num == 1) {
document.getElementById("bod").style.color = 'black';
document.body.style.backgroundColor = 'yellow';
document.getElementById("another").style.backgroundColor = 'yellow';
document.getElementById("another").style.color = 'black';
document.getElementById("tweet").style.color = 'black';
document.getElementById("tweet").style.backgroundColor = 'yellow';
}
if (num == 2) {
document.getElementById("bod").style.color = 'white';
document.body.style.backgroundColor = 'blue';
document.getElementById("another").style.backgroundColor = 'blue';
document.getElementById("tweet").style.backgroundColor = 'blue';
}
}
function twitter() {
text = document.getElementById('quote').innerHTML;
// document.getElementById('tw').innerHTML = text;
$('.twitter-button').attr("href", 'https://twitter.com/intent/tweet?text=' + text);
};

<body id='bod'>
<h1>Quotes</h1>
<div class='text'>
<p id='quote'>You're fired!</p>
<br>
<p id='author'>Vince McMahon</p>
<button id='another' class='btn btn-primary' onclick='change()'>Another Quote</button>
<br>
<br>
<a class="twitter-button" href="https://twitter.com/intent/tweet?text=" target="_blank">
<button type="button" onclick='twitter()' class="btn btn-primary" id='tw'>Tweet!</button>
</a>
&#13;
每当我点击推文按钮时,Twitter页面上都没有文字。只是一个空白框。
指向Codepen的链接 - https://codepen.io/pranavgoel25/pen/ZybNRv?editors=1010
答案 0 :(得分:0)
我已经调整了一下代码:
var quotes = ["Naam toh suna hi hoga","Mogambo Khush Hua","Kitne aadmi the?"];
function change(){
auth = ["Raj","Mogambo","Gabbar"];
min = 0;
max = 3;
num = Math.floor(Math.random() * (max - min)) + min;
q = quotes[num]
document.getElementById("quote").innerHTML = quotes[num];
document.getElementById("author").innerHTML = auth[num];
document.getElementById('twitterBtn').setAttribute("href", 'https://twitter.com/intent/tweet?text=twee' + quotes[num]);
if (num == 0){
document.getElementById("bod").style.color='white';
document.body.style.backgroundColor = 'green';
document.getElementById("another").style.backgroundColor='green';
document.getElementById("tweet").style.backgroundColor='green';
}
if (num == 1){
document.getElementById("bod").style.color='black';
document.body.style.backgroundColor = 'yellow';
document.getElementById("another").style.backgroundColor='yellow';
document.getElementById("another").style.color='black';
document.getElementById("tweet").style.color='black';
document.getElementById("tweet").style.backgroundColor='yellow';
}
if (num == 2){
document.getElementById("bod").style.color='white';
document.body.style.backgroundColor = 'blue';
document.getElementById("another").style.backgroundColor='blue';
document.getElementById("tweet").style.backgroundColor='blue';
}
}
window.onload = function(){
document.getElementById('twitterBtn').setAttribute("href", 'https://twitter.com/intent/tweet?text=twee' + quotes[0]);
}