Javascript 如何使变量不开始新行

Javascript 如何使变量不开始新行,javascript,Javascript,我正在制作一个网站,在那里你必须输入一个链接,它生成javascript,但是在输入变量后,它添加了一行,所以它不起作用 这是我正在使用的代码 var url = prompt("Enter the link you want the app to go to."); document.getElementById(2).onclick = "window.open('" + url + "', '_blank');'>"; 有人有什么建议吗?我想你想做的就是这样 var

我正在制作一个网站,在那里你必须输入一个链接,它生成javascript,但是在输入变量后,它添加了一行,所以它不起作用

这是我正在使用的代码

    var url = prompt("Enter the link you want the app to go to.");
    document.getElementById(2).onclick = "window.open('" + url + "', '_blank');'>";

有人有什么建议吗?

我想你想做的就是这样

var url = prompt("Enter the link you want the app to go to.");
if(url){
    document.getElementById("xxxx").onclick=function(){
    window.open(url, '_blank');
  }
}

您不需要使用这些字符串在window.open方法中设置url变量

你可以试试这个:

 var url = prompt("Enter the link you want the app to go to.");

 document.getElementById(2).onclick = () => window.open(url, '_blank');

最快的修复方法:首先移除这个杂散的
,看起来你的窗口中可能有一个杂散的
。打开呼叫,这可能会把事情搞砸。不确定“添加一行”是什么意思,你能澄清一下吗?我需要字符串进入HTML onclick属性
var url = prompt("Enter the link you want the app to go to.");

document.getElementById(2).onclick = new Function("window.open('" + url + "', '_blank');")