HTML DOM Location 搜索属性

htmlweb developmentfront end technology更新于 2025/9/19 12:28:17

Location 搜索属性返回/设置与 URL 查询参数对应的字符串。

语法

以下是语法 −

  • search  属性的返回值
location.search
  • 协议属性集的值
location.search = searchString

示例

让我们看一个 Location 搜索 属性的示例 −

<!DOCTYPE html>
<html>
<head>
<title>Location protocol</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>Location-protocol</legend>
<label for="urlSelect">Current URL:</label>
<input type="url" size="30" id="urlSelect" value="https://www.example.com:2544/aboutUs/CTO.htm">
<input type="button" onclick="getprotocol()" value="Get protocol">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var urlSelect = document.getElementById("urlSelect");
   function getprotocol(){
      divDisplay.textContent = 'URL protocol: '+location.protocol;
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

点击"获取搜索值"按钮前 −

点击"获取搜索值"按钮后 −


相关文章


有用资源