HTML DOM Title text 属性
htmlweb developmentfront end technology更新于 2024/3/19 9:16:17
HTML DOM Title text 属性设置/返回文档中 <title> 元素的值。
以下是语法 −
返回字符串值
titleElementObject.text
将 text 设置为字符串值
titleElementObject.text = string
让我们看一个 Title text 属性的示例 −
示例
<!DOCTYPE html>
<html>
<head>
<title id="titleSelect">HTML DOM Title text</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 id="legendSelect"></legend>
<input type="button" onclick="getTitleText()" value="What's the title of document?">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var legendSelect = document.getElementById("legendSelect");
var titleSelect = document.getElementById("titleSelect");
function getTitleText() {
divDisplay.textContent = 'Title of document: '+titleSelect.text;
legendSelect.textContent = titleSelect.text.split(' ').join('-');
}
</script>
</body>
</html>
输出
点击"文档的标题是什么?"按钮之前 −

点击"文档的标题是什么?"按钮之后 −

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

