Я хочу сделать ссылку, где href зависит от переменной js - PullRequest
0 голосов
/ 14 февраля 2020

Я понял всю внутреннюю вещь html, но я не знаю, как добавить переменные в 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>

Ответы [ 2 ]

1 голос
/ 14 февраля 2020

Вы можете установить href atrubite через <element>.setAttribute("href", <value>)
-> document.getElementById("title").setAttribute("href", js_input)
(Как уже упоминалось Дэвидом: <element>.title = <value> также работает)

0 голосов
/ 14 февраля 2020

<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 в последней строке. Также используйте .href для изменения значения href. Надеюсь, это поможет!
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...