繁体   English   中英

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

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

我想通了整个innerhtml,但我不知道如何将变量添加到href 中。 我希望它制作一个链接,标题是您输入的标题,而当您单击“创建”时,您输入的 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>

您可以通过<element>.setAttribute("href", <value>)设置href atrubite
-> document.getElementById("title").setAttribute("href", js_input)
(正如大卫所提到的: <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>
您在最后一行的 getElementById 中使用了错误的 id。 此外,使用 .href 更改 href 值。 希望这可以帮助!

暂无
暂无

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

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