当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js坚持不懈之16:使用js向HTML元素分配事件

js坚持不懈之16:使用js向HTML元素分配事件

2019年02月20日  | 移动技术网IT编程  | 我要评论

向 button 元素分配 onclick 事件:

<!doctype html>
<html>
<body>

<p>点击按钮就可以执行 <em>displaydate()</em>函数。</p>

<button onclick = "displaydate()">点击点击点击</button>

<script>
function displaydate()
{
    document.getelementbyid("demo").innerhtml = date();
}
</script>

<p id = "demo">会展示日期</p>

</body>
</html>

 2.

<!doctype html>
<html>
<body onload = "checkcookies()">

<script>
    function checkcookies()
    {
        try
        {
            if (navigator.cookieenabled == true)
            {
                alert("已启用 cookie")
            }
            else
            {
                alert("未启用 cookie")
            }
        }
        catch (err)
        {
            alert('asdf')
        }
    }

</script>

<p>提示框会告诉你,浏览器是否已启用 cookie。</p>
</body>
</html>

3. 

<!doctype html>
<html>
<head>
<script>
function myf()
{
    var x = document.getelementbyid("fname");
    x.value = x.value.touppercase();
}
</script>
</head>

<body>
请输入应为字符:<input type = "text" id = "fname" onchange = "myf()">
<p>当您离开输入字段时,会触发将输入文本转换为大写的函数。</p>
</body>
</html>

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网