簡體   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