HTML DOM 輸入 URL 值屬性


如果為 input URL 定義了 value 屬性,HTML DOM 輸入 URL 值屬性會返回一個字串。使用者還可以將其設定為新的字串。

語法

以下是語法 -

  • 返回字串值
inputURLObject.value
  • 將 value 屬性設定為字串值
inputURLObject.value = ‘String’

示例

讓我們看一個輸入 URL 值 屬性的示例 -

 即時演示

<!DOCTYPE html>
<html>
<head>
<title>Input URL value</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>URL-value</legend>
<label for="URLSelect">URL Id:
<input type="url" id="URLSelect">
<input type="button" onclick="getUserURL('google')" value="Google">
<input type="button" onclick="getUserURL('bing')" value="Bing"><br>
<input type="button" onclick="redirection()" value="Go">
</label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputURL = document.getElementById("URLSelect");
   function getUserURL(userName) {
      if(userName === 'google')
         inputURL.value = 'https://www.google.com';
      else
         inputURL.value = 'https://www.bing.com';
   }
   function redirection() {
      if(inputURL.value !== '')
         divDisplay.textContent = 'Redirecting to '+inputURL.value;
      else
         divDisplay.textContent = 'Enter URL';
   }
</script>
</body>
</html>

輸出

這將產生以下輸出 -

點選 URL 欄位為空時的“轉到”按鈕 -

點選“必應”按鈕後 -

點選 URL 欄位已設定時的“轉到”按鈕 -

更新時間:30-7 -2019

103 views

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.