简体   繁体   English

JavaScript <a>href 替换不起作用</a>

[英]JavaScript <a> href replacing is not working

I'm making a font download resource website, and this is my JS and some of my html:我正在制作一个字体下载资源网站,这是我的 JS 和我的一些 html:

 var element, text, font, download, value; function tick() { element = document.getElementById("text"); font = document.getElementById("fontfamily"); text = document.getElementById("dummy-text"); download = document.getElementById("downlink"); text.innerHTML = element.value; text.style.fontFamily = font.value; value = font.value + ".tff"; download.setAttribute("href", value); }
 <input type="text" id="text" value="Lorem ipsum dolor set amit."> <select id="fontfamily"> <optgroup label="Apple"> <option>Helvetica Neue</option> </optgroup> <optgroup label="Google"> <option>Roboto Regular</option> <option>Roboto Light</option> <option>Roboto Bold</option> <option>Roboto Italic</option> </optgroup> <optgroup label="Monotype"> <option>Century Gothic</option> </optgroup> <optgroup label="Nintendo"> <option>Wii Sans Regular</option> <option value="SMW">Super Mario World Sans</option> </optgroup> </select><button onclick="tick()">Set Text</button> <br><br> <h1 id="dummy-text" style="font-family: Helvetica Neue;">Lorem ipsum dolor set amit.</h1><a href="Helvetica Neue.ttf" id="downlink"><button style="margin: 0;">Download</button></a>

but when I press "Download", the link I went to was [object%20HTMLSelectElement].tff, which obviously does not exist.但是当我按下“下载”时,我转到的链接是[object%20HTMLSelectElement].tff,这显然不存在。

Any help?有什么帮助吗?

The problem that you have at the moment, is that you are setting an attribute, not setting the href.您目前遇到的问题是您正在设置属性,而不是设置 href。 To set your href do设置你的 href 做

let download = document.getElementById("downlink");
download.href = "https://example.com";

and that should fix it!这应该解决它!

Note: I noticed you're a new member!注意:我注意到你是一个新成员! First of all, welcome to StackOverflow.首先,欢迎来到 StackOverflow。 Second of all, be sure to mark the answer of your questions by clicking the check next to the answer that helped you solve it so people know this question got it's answer and someone new can also find it easily!其次,请务必通过单击帮助您解决问题的答案旁边的对勾来标记您的问题的答案,以便人们知道此问题得到了答案,新人也可以轻松找到它!

After running the snippet I see that even if it is setting the correct value to href , it is getting URL encoded when you click on the Download button.运行代码段后,我看到即使将正确的值设置为href ,当您单击“ Download按钮时,它也会对URL encoded

And that's how Roboto Regular will become Roboto%20Regular .这就是Roboto Regular将变成Roboto%20Regular

Add download attribute to <a> tag to specify that the target will be downloaded when a user clicks on the hyperlink.download属性添加到<a>标记以指定当用户单击超链接时将下载目标。

 var element, text, font, download, value; function tick() { element = document.getElementById("text"); font = document.getElementById("fontfamily"); text = document.getElementById("dummy-text"); download = document.getElementById("downlink"); text.innerHTML = element.value; text.style.fontFamily = font.value; value = font.value + ".tff"; download.href = value; }
 <input type="text" id="text" value="Lorem ipsum dolor set amit."> <select id="fontfamily"> <optgroup label="Apple"> <option>Helvetica Neue</option> </optgroup> <optgroup label="Google"> <option>Roboto Regular</option> <option>Roboto Light</option> <option>Roboto Bold</option> <option>Roboto Italic</option> </optgroup> <optgroup label="Monotype"> <option>Century Gothic</option> </optgroup> <optgroup label="Nintendo"> <option>Wii Sans Regular</option> <option value="SMW">Super Mario World Sans</option> </optgroup> </select><button onclick="tick()">Set Text</button> <br><br> <h1 id="dummy-text" style="font-family: Helvetica Neue;">Lorem ipsum dolor set amit.</h1><a href="Helvetica Neue.ttf" id="downlink"><button style="margin: 0;" download>Download</button></a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM