HTML DOM Location port 属性
htmlweb developmentfront end technology更新于 2025/7/18 12:28:17
Location port 属性返回/设置 URL 的端口号(如果指定)。如果未明确指定,端口号可能不会显示。
语法
以下是语法 −
- port 属性的返回值
location.port
- port 属性设置的值
location.port = portNumber
示例
让我们看一个 Location port 属性的示例 −
<!DOCTYPE html>
<html>
<head>
<title>Location port</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-port</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="getport()" value="Get Port">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var urlSelect = document.getElementById("urlSelect");
function getport(){
divDisplay.textContent = 'URL port: '+location.port;
}
</script>
</body>
</html>
输出
这将产生以下输出 −
点击"获取端口"按钮之前 −

点击"获取端口"按钮之后 −

相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

