【问题标题】:Tweet text from a page来自页面的推文文本
【发布时间】:2017-11-11 00:54:12
【问题描述】:

我想从 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>

每当我点击推特按钮时,推特页面上都没有文字。只是一个空白框。

链接到 Codepen - https://codepen.io/pranavgoel25/pen/ZybNRv?editors=1010

【问题讨论】:

    标签: javascript jquery html twitter


    【解决方案1】:

    我已经修改了一个代码位:

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

    工作演示: https://codepen.io/anon/pen/BZjQBe?editors=1010

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      相关资源
      最近更新 更多