【问题标题】:Create dynamic link in javascript?在javascript中创建动态链接?
【发布时间】:2017-10-11 22:52:05
【问题描述】:

我正在尝试创建一个 twitter 按钮,允许用户分享随机报价生成器的结果。
这是我的html:

报价机

 <button onclick="generateQuote()">Click me</button> 

 <p id="quotes"></p>

 <p id ="twitter"></p>

这是我的 javascript:

              var quotearray =["Always be yourself","Stay in the moment","Be merry"];
 var pickAQuote = function () {
var newquote = quotearray[Math.floor(Math.random() * 3)];
var noquotes = newquote.replace(/['"]+/g, '');
return noquotes;
};


function generateQuote() {
var newquotes = pickAQuote();

document.getElementById("quotes").innerHTML= newquotes;  
document.getElementById("twitter").innerHTML ='<button onclick=' + 
  '"https://twitter.com/intent/tweet?text=' + newquotes + '";>Tweet 
This</button>';



  }

因此,无论随机生成的报价是什么,用户都可以在 Twitter 上分享该报价。但我无法让它工作!任何建议将不胜感激!

【问题讨论】:

标签: javascript html dom button twitter


【解决方案1】:

如果您想使用 onClick 事件更改 url,您应该执行以下操作:

document.getElementById("twitter").innerHTML ='<button onclick="location.href = \'https://twitter.com/intent/tweet?text=' + newquotes + '\'";>Tweet This</button>';

【讨论】:

    猜你喜欢
    • 2023-04-06
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-22
    • 1970-01-01
    • 2016-01-13
    相关资源
    最近更新 更多