Web前端开发笔记38——事件三要素、操作元素、样式修改、属性值的获取设置和移除

本文介绍了如何使用JavaScript进行DOM操作,包括改变元素内容、修改属性及表单属性等实用技巧,并展示了如何通过修改样式实现动态效果。

1.改变元素内容:

2.innerText和innerHTML区别:

innerText不识别元素标签,但是innerHTML可以。

3.修改属性:

4.修改表单属性:

5.this的使用,可以使用this来表示当前对象。

var textPut = document.getElementById('textline');
    var btn1 = document.getElementById('btn1');
    btn1.onclick = function () {
        textPut.value = 'i changed!';
        this.disabled = true;
    }

6.通过修改input的type来修改其为text或者password类型。

7.css样式的修改示例如下:

var div1 = document.querySelector('#div1');
    var flag = 0;
    div1.onmouseover = function () {
        if (flag == 0) {
            this.style.fontSize = '30px';
            this.style.color = 'rgb(224,142,142)';
            this.style.backgroundColor = 'rgb(198,174,59)';
            flag = 1;
        }

    }
    div1.onmouseout = function () {
        if (flag == 1) {
            this.style.fontSize = '20px';
            this.style.color = 'rgb(198,174,59)';
            this.style.backgroundColor = 'rgb(181, 224, 209)';
            flag = 0;
        }
    }

8.但是,我们如果出现样式很多的情况一般不这样写,我们可以选择className修改样式属性:

我们可以预先写一个样式表,然后使用element.className='样式名';来调用即可。示例:

var div1 = document.querySelector('#div1');
    var flag = 0;
    div1.onmouseover = function () {
        if (flag == 0) {
            this.className = 'div1 mouseon';
            flag = 1;
        }
    }
    div1.onmouseout = function () {
        if (flag == 1) {
            this.className = 'div1 mouseout';
            flag = 0;
        }
    }

9.获取元素的属性值:element.属性名。

10.设置属性:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

言行物恒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值