Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.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_Css_Web - Fatal编程技术网

Javascript 如何将模板字符串添加到链接

Javascript 如何将模板字符串添加到链接,javascript,html,css,web,Javascript,Html,Css,Web,所以基本上我想知道的是,你是否可以在一个链接中添加一个模板字符串(这个东西${}),来改变链接的内容。在上下文中,单击一个按钮,我想生成一个图像,图像源是一个链接,但我想输入一个文本,将链接添加到一个特定的部分,如果有意义的话,它会改变图像结果。我已经想出了这个代码,但它不起作用 这是我点击按钮的代码 const shoesimg = document.createElement('img'); const shoesinput = document.getElementById('shoesi

所以基本上我想知道的是,你是否可以在一个链接中添加一个模板字符串(这个东西
${}
),来改变链接的内容。在上下文中,单击一个按钮,我想生成一个图像,图像源是一个链接,但我想输入一个文本,将链接添加到一个特定的部分,如果有意义的话,它会改变图像结果。我已经想出了这个代码,但它不起作用

这是我点击按钮的代码

const shoesimg = document.createElement('img');
const shoesinput = document.getElementById('shoesinput');

generate.addEventListener('click', function(){

    shoesimg.src = "https://stockx-360.imgix.net//" `${shoesinput.value}` "/Images/" `${shoesinput.value}` "/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806"
});
一个例子是:


您应该使整个链接成为模板字符串

const shoesimg = document.createElement('img');
const shoesinput = document.getElementById('shoesinput');

generate.addEventListener('click', function(){

    shoesimg.src = `https://stockx-360.imgix.net//${shoesinput.value}/Images/${shoesinput.value}/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806`
});

将整个字符串设置为模板字符串?以反勾号开始整个字符串,不包括任何其他引号,然后以另一个反勾号结束整个字符串。看起来您试图使用普通字符串连接和使用模板进行组合,而实际上您只需要使用一个模板。另一个可行的方法是在每个字符串/模板之间添加连接符号
+
,但这会破坏使用模板的整个目的。
const shoesimg = document.createElement('img');
const shoesinput = document.getElementById('shoesinput');

generate.addEventListener('click', function(){

    shoesimg.src = `https://stockx-360.imgix.net//${shoesinput.value}/Images/${shoesinput.value}/Lv2/img01.jpg?auto=format,compress&w=559&q=90&dpr=2&updated_at=1580325806`
});