Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/466.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中创建动态链接?_Javascript_Html_Dom_Button_Twitter - Fatal编程技术网

在javascript中创建动态链接?

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

我正在尝试创建一个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)];
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>';