JavaScript设置或获取元素标题|Element title 属性

元素对象参考手册


定义与用法

title 属性设置或返回元素的标题。


语法格式

HTMLElementObject.title= title


示例代码

返回 body 元素的 title属性:

<html>
    <body id="myid" title="mytitle">
        <script>
            var x = document.getElementsByTagName('body')[0];
            document.write("Body title: " + x.title);
            document.write("<br>");
            document.write("An alternate way: ");
            document.write(document.getElementById('myid').title);
        </script>
    </body>
</html>

输出结果:

Body title: mytitle
An alternate way: mytitle

亲自试试


浏览器支持

属性Google ChromeInternet ExplorerFirefoxSafariOpera
titleYesYesYesYesYes

所有主要浏览器都支持 title 属性


更多示例

返回图像中的区域地图标题:

<!DOCTYPE html>
<html>
    
    <head>
        <meta charset="utf-8">
        <title>奔月教程(runoon.com)</title>
    </head>
    
    <body>
        <img src="/img/solar.jpg" border="0" usemap="#planetmap" alt="太阳系">
        <map name="planetmap" id="planetmap">
            <area id="earth" shape="circle" coords="332,260,51" title="地球" href="/uncategorized/earth/" alt="地球">
            <area id="jupiter" shape="circle" coords="572,160,61" title="木星" href="uncategorized/jupiter/" alt="木星"></map>
        <script>
            var x = document.getElementById('earth');
            document.write(x.title);
        </script>
    </body>

</html>

亲自试试


元素对象参考手册


相关