在javascript中创建动态链接?
我正在尝试创建一个twitter按钮,允许用户共享随机报价生成器的结果。在javascript中创建动态链接?,javascript,html,dom,button,twitter,Javascript,Html,Dom,Button,Twitter,我正在尝试创建一个twitter按钮,允许用户共享随机报价生成器的结果。 这是我的html: 报价机 点击我 这是我的javascript: var quotearray =["Always be yourself","Stay in the moment","Be merry"]; var pickAQuote = function () { var newquote = quotearray[Math.floor(Math.random() * 3)];
这是我的html: 报价机
点击我
这是我的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>';
}
var quotearray=[“永远做你自己”、“停留在当下”、“快乐起来”];
var pickAQuote=函数(){
var newquote=quotearray[Math.floor(Math.random()*3)];
var noquotes=newquote.replace(/['“]+/g');
返回noquotes;
};
函数generateNote(){
var newquotes=pickAQuote();
document.getElementById(“quotes”).innerHTML=newquotes;
document.getElementById(“twitter”).innerHTML='Tweet
这",;
}
因此,我们的想法是,无论随机生成的报价是什么,用户都可以在twitter上共享该报价。但我无法让它发挥作用!任何建议都将不胜感激!如果您想使用onClick事件更改url,您应该执行以下操作:
document.getElementById("twitter").innerHTML ='<button onclick="location.href = \'https://twitter.com/intent/tweet?text=' + newquotes + '\'";>Tweet This</button>';
document.getElementById(“twitter”).innerHTML='Tweet This';
onclick
不会将您带到另一个网站,它会执行JavaScript。您可以使用window.location.href=
或使用锚元素替换按钮。可能重复
document.getElementById("twitter").innerHTML ='<button onclick="location.href = \'https://twitter.com/intent/tweet?text=' + newquotes + '\'";>Tweet This</button>';