简体   繁体   English

我想建立一个链接,其中 href 依赖于 js 变量

[英]I want to make a link where the href is dependent on a js variable

I figured out the whole innerhtml thing but I don't know how to add variables into the href.我想通了整个innerhtml,但我不知道如何将变量添加到href 中。 I want it to make a link with the title being the title you enter and with the javascript that you enter to be in the href when you click "create".我希望它制作一个链接,标题是您输入的标题,而当您单击“创建”时,您输入的 javascript 将位于 href 中。

 <div class="title"> Bookmarklet Maker <br> <br> <input size="13" onkeypress="return searchKeyPress(event);" name="getTitle" type="text" id="getTitle" style="background-color: #252525; border: 2px; border-style: double; border-color: white; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor:;" placeholder="Bookmarklet name" /> <br> <input size="40" onkeypress="return searchKeyPress(event);" name="geJs" type="text" id="getJs" style="background-color: #252525; border: 2px; border-style: double; border-color: white; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor:;" placeholder="Javascript" /> <br> <button style="background-color: #252525; border: 2px; border-style: solid; border-color: white; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor: pointer;" id="bookmarkletbutton" onclick="createBookmarklet()">Create</button> <script> function createBookmarklet() { var title_input = document.getElementById('getTitle').value; var js_input = document.getElementById('getJs').value; document.getElementById('title').innerHTML = title_input; document.getElementById('js').innerHTML = js_input; } </script> <br> <br> <a class="link" href="" id="title"></a> </div>

You can set the href atrubite via <element>.setAttribute("href", <value>)您可以通过<element>.setAttribute("href", <value>)设置href atrubite
-> document.getElementById("title").setAttribute("href", js_input) -> document.getElementById("title").setAttribute("href", js_input)
(As mentioned by David: <element>.title = <value> also works) (正如大卫所提到的: <element>.title = <value>也有效)

 <div class="title"> Bookmarklet Maker <br> <br> <input size="13" name="getTitle" type="text" id="getTitle" style="background-color: #252525; border: 2px; border-style: double; border-color: white; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor:;" placeholder="Bookmarklet name" /> <br> <input size="40" name="geJs" type="text" id="getJs" style="background-color: #252525; border: 2px; border-style: double; border-color: white; color: white; padding: 15px 32px; text-align: left; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor:;" placeholder="Javascript" /> <br> <button style="background-color: #252525; border: 2px; border-style: solid; border-color: white; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; font-weight: bold; margin: 4px 2px; cursor: pointer;" id="bookmarkletbutton" onclick="createBookmarklet()">Create</button> <script> function createBookmarklet() { var title_input = document.getElementById('getTitle').value; var js_input = document.getElementById('getJs').value; document.getElementById('title').innerHTML = title_input; document.getElementById('title').href = js_input; } </script> <br> <br> <a class="link" href="" id="title"></a> </div>
You were using the wrong id in getElementById in the last line. 您在最后一行的 getElementById 中使用了错误的 id。 Also, use .href for changing href value. 此外,使用 .href 更改 href 值。 Hope this helps! 希望这可以帮助!

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

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