HTML DOM 位置端口属性
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>输出
将产生以下输出 -
在点击 '获取端口' 按钮之前 -

在点击 '获取端口' 按钮之后 -

广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP