16.事件及属性操作

19 篇文章 0 订阅

javascript基础

事件

###事件:触发-响应机制

事件三要素

  • 事件源:触发(被)事件的元素
  • 事件名称: click 点击事件
  • 事件处理程序:事件触发后要执行的代码(函数形式)

事件的基本使用

var box = documen.getElementById('box');
box.onclick = function() {
  console.log('代码会在box被点击后执行');
}

属性操作

非表单属性

href、title、id、src、className

​ 美女相册

 <style>
    * {
      margin: 0;
      padding: 0;
    }
    .mar20 {
      margin-left: 20px;
    }
    html,
    body {
      width: 100%;
      height: auto;
      text-align: center;
      background-color: #CCCCCC;
    }
    .span {
      display: block;
      height: 20px;
      width: 300px;
    }
  </style>
</head>
<body>
  <div id = "box">
    <h1>宝贝BOSS</h1>
    <span class = "span"></span>
    <div id = "boss">
      <a href="img/dearboss1.jpg" title = "宝贝1">
        <img src = "img/dearboss1.jpg" width = "74" height = "46" alt = "宝贝1">
      </a>
      <a href="img/dearboss2.jpg" class ="mar20" title = "宝贝1">
        <img src = "img/dearboss2.jpg" width = "74" height = "46" alt = "宝贝2">
      </a>
      <a href="img/dearboss3.jpg" class ="mar20" title = "宝贝1">
        <img src = "img/dearboss3.jpg" width = "74" height = "46" alt = "宝贝3">
      </a>
      <a href="img/dearboss1.jpg" class ="mar20" title = "宝贝1">
        <img src = "img/dearboss1.jpg" width = "74" height = "46" alt = "宝贝4">
      </a>
    </div>
    <span class = "span"></span>
    <div> 
      <img src = "img/boss.png" id = "bigimage" width = "298" height = "186" alt = "宝贝老板">
      <p id = "remind">请点击需要查看的图片</p>
    </div>
  </div>
  <script>
    var boss = document.getElementById('boss');
    var as = boss.getElementsByTagName('a');
    for(i = 0; i < as.length; ++i ) {
      var A = as[i];
      A.onclick = function () {
      var remind = document.getElementById('remind');
      var bigimage = document.getElementById('bigimage');
      remind.innerText = this.title;
      bigimage.src = this.href;
      return false;      //取消后续内容的执行
    }
  }
  </script>
</body>

总结
  • this. 表示当前被调用的函数

  • div标签的class在javascript中用classname表示; div标签的描述值用value

  • P标签的文本描述在javascript中用 innerText;

  • 判断字符串是否为空,一般判断他的长度是否为0;

  • innerHTML和innerText

var box = document.getElementById('box');
box.innerHTML = '我是文本<p>我会生成为标签</p>';
console.log(box.innerHTML);
box.innerText = '我是文本<p>我不会生成为标签</p>';
console.log(box.innerText);
  • HTML转义符
"		&quot;
'		&apos;
&		&amp;
<		&lt;   // less than  小于
>		&gt;   // greater than  大于
空格	   &nbsp;
©		&copy;

  • innerHTML和innerText的区别,都是双标签的属性

    innerHTML返回的结果: 如果内容中有标签,会把标签也获取到

    innnerText 返回的结果: 如果内容中有标签,会把标签过滤到,前后的换行和空白都去掉.

  • innerText的兼容性处理

    <body>
      <div id = "box">
        hello
      </div>
      <script>
        var box = document.getElementById('box');
        console.log(getInnerText(box);
        function getInnerText(element) {
          if(typeof element.innerText == 'string') {
            return element.innerText;
          } else {
            return element.textContent;
          }
        }
       </script>
     </body>
    
    

注意:

设置内容的时候,下面两个都可以拿来设置,但是当设置不含标签内容的时候,应该使用innerText

innerText(textContent)

innerHTML

表单元素属性

  • value 用于大部分表单元素的内容获取(option除外)
  • type 可以获取input标签的类型(输入框或复选框等)
  • disabled 禁用属性 只有一个值,属于布尔类型
  • checked 复选框选中属性 只有一个值,属于布尔类型
  • selected 下拉菜单选中属性 只有一个值,属于布尔类型

案例

  • 点击按钮禁用文本框

    <body>
      <button id = "button1">开启</button>
      <button id = "button2">禁用</button>
      <input  type = "password" id = "INPUT">
      <script>
        var INPUT = document.getElementById('INPUT');
        var button1 = document.getElementById('button1');
        var button2 = document.getElementById('button2');
        button1.onclick = function() {
          INPUT.disabled = false;
        }
        button2.onclick = function() {
          INPUT.disabled = true;
        }
      </script> 
    </body>
    
    
  • 给文本框赋值,获取文本框的值

    <body>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input type="text"><br>
      <input id="btn" type="button" value="获取文本框的值">
    </body>
    <script>
      var inputs = document.getElementsByTagName('input');
      var btn = document.getElementById('btn');
      btn.onclick = function () {
          var array = [];
          for (var i = 0; i < inputs.length; i++) {
            var input = inputs[i];
            if (input.type == 'text') {
               array.push(input.value);
            }
          }
          console.log(array.join('|'));
        }
     
    </script>
    
    
  • 检测用户名是否是3-6位,密码是否是6-8位,如果不满足要求高亮显示文本框

     <style>
        .bg {
          background-color: yellow;
        }
      </style>
    </head>
    <body>
      <p>账户</p>
      <input type="text" id = "butt1"><br>
      <p>密码</p>
      <input type="password" id = "butt2"><br>
      <p></p>
      <input type="button" id = "butt3" value = "登录">
      <script>
      var butt1 = document.getElementById('butt1');
      var butt2 = document.getElementById('butt2');
      var butt3 = document.getElementById('butt3');
      butt3.onclick = function() {
        if((butt1.value).length >= 3 || (butt1.value).length <= 6) {
          butt1.className = "bg";
        }
        if((butt2.value).length >= 6 || (butt2.value).length <= 8) {
          butt2.className = "bg";
        }
      }
      </script>
    
    
  • 设置下拉框中的选中项

     <input type="button" value="设置" id='btnSet'>
     <select id="selCities">
        <option value="1">北京</option>
        <option value="2">上海</option>
        <option value="3">杭州</option>
        <option value="4">郑州</option>
        <option value="5">武汉</option>
      </select>
      <script>
        var btnSet = document.getElementById('btnSet');
        btnSet.onclick = function () {
          var selCities = document.getElementById('selCities');
          var options = selCities.getElementsByTagName('option');
        
          // Math.random() -> [0, 1)
          // Math.random() * 5 -> [0, 5)
          var randomIndex = parseInt(Math.random() * options.length);
          // 4 根据索引获取option,并让option选中
          var option = options[randomIndex];
          option.selected = true;
        }
      </script>
    
    
  • 搜索文本框

     <style>
        .red {
          color: red;
        }
        .black {
          color: green;
        }
      </style>
    </head>
    <body>
      <input type = "text" id = "textinput" value = "请输入搜索关键字">
      <input type = "button" id = "buttoninput" value = "搜索"> 
      <script>
      
      var textinput = document.getElementById('textinput');
        var buttoninput = document.getElementById('buttoninput');
        textinput.onfocus = function () {
          if (this.value === '请输入搜索关键字') {
            this.value = '';
            this.className = 'red';
          }
        }
        textinput.onblur = function () {
          if (this.value.length === 0 || this.value === '请输入搜索关键字') {
            this.value = '请输入搜索关键字';
            this.className = 'gray';
          }
        }
      </script>
    
    
  • 全选反选

        <style>
            * {
                padding: 0;
                margin: 0;
            }
    
            .wrap {
                width: 300px;
                margin: 100px auto 0;
            }
    
            table {
                border-collapse: collapse;
                border-spacing: 0;
                border: 1px solid #c0c0c0;
                width: 300px;
            }
    
            th,
            td {
                border: 1px solid #d0d0d0;
                color: #404060;
                padding: 10px;
            }
    
            th {
                background-color: #09c;
                font: bold 16px "微软雅黑";
                color: #fff;
            }
    
            td {
                font: 14px "微软雅黑";
            }
    
            tbody tr {
                background-color: #f0f0f0;
            }
    
            tbody tr:hover {
                cursor: pointer;
                background-color: #fafafa;
            }
        </style>
    
    </head>
    <body>
      <div class="wrap">
          <table>
              <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="j_cbAll" />
                    </th>
                    <th>商品</th>
                    <th>价钱</th>
                </tr>
              </thead>
              <tbody id="j_tb">
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPhone8</td>
                    <td>8000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Pro</td>
                    <td>5000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>iPad Air</td>
                    <td>2000</td>
                </tr>
                <tr>
                    <td>
                        <input type="checkbox" />
                    </td>
                    <td>Apple Watch</td>
                    <td>2000</td>
                </tr>
    
              </tbody>
          </table>
          <input type="button" value="  反 选  " id="btn">
      </div>
      <script>
         var j_tb = document.getElementById('j_tb');
         var inputs = j_tb.getElementsByTagName('input');
         var j_cbAll = document.getElementById('j_cbAll');
         j_cbAll.onclick = function () {
           for (var i = 0; i < inputs.length; i++) {
                var input = inputs[i];
                input.checked = this.checked;
            }
        }
        for (var i = 0; i < inputs.length; i++) {
           var input = inputs[i];
           input.onclick = function () {
             checkAllCheckBox();
           }
         }
        function checkAllCheckBox() {
            var isAllChecked = true;
            for (var i = 0; i < inputs.length; i++) {
               var input = inputs[i];
               if (!input.checked) {
                 isAllChecked = false;
               }
            }
             j_cbAll.checked = isAllChecked;
        }
         var btn = document.getElementById('btn');
         btn.onclick = function () {
            for (var i = 0; i < inputs.length; i++) {
              var input = inputs[i];
              input.checked = !input.checked;
              checkAllCheckBox();
            }
         }
      </script>
      <!-- 1.设置全选按钮与子按钮的状态一致
           2.判断子按钮是否全部被选中,如果未被全部选中,全选按钮必须为不选中
           3.设置反选按钮的状态与自按钮的状态相反.
            -->
    </body>
    
    
总结知识点:
. 当判断字符串是否为空时,一般都是判断字符串的长度
变量名.onclick = function () { } 表示点击变量时,执行函数
变量名.onfocus = function () { } 表示 聚焦变量时,执行函数
变量名.onblur = function () { } 表示变量失去焦点时,执行函数
if (!input.checked) {} 判断一个复选框是否被选中,如果未被选中,执行函数

自定义属性操作

  • getAttribute() 获取标签行内属性

  • setAttribute() 设置标签行内属性

  • removeAttribute() 移除标签行内属性

  • 与element.属性的区别: 上述三个方法用于获取任意的行内属性。

    <div id = "box" age = "18" personId = "1">张三</div
    <script>
      var box = document.getElementById('box');
      console.log(box.getAttribute('age'));
      box.setAttribute('sex','male');
      box.removeAttribute('age');
    </script>
    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据你提供的分类思路,以下是扩展的各个子分类,每个分类有20条扩展内容: 显象: 1. 直接调用的类名 2. 直接调用的文件名 3. 直接调用的函数名 4. 直接调用的模块名 5. 直接调用的库名 6. 直接调用的变量名 7. 直接调用的常量名 8. 直接调用的宏定义 9. 直接调用的数据结构名 10. 直接调用的枚举类型名 11. 直接调用的命令行参数 12. 直接调用的配置文件名 13. 直接调用的网络地址 14. 直接调用的数据库表名 15. 直接调用的外部API名 16. 直接调用的系统调用名 17. 直接调用的GUI组件名 18. 直接调用的UI界面元素名 19. 直接调用的web服务端点名 20. 直接调用的第三方库函数名 真象: 1. 隐含调用的类名 2. 隐含调用的文件名 3. 隐含调用的函数名 4. 隐含调用的模块名 5. 隐含调用的库名 6. 隐含调用的变量名 7. 隐含调用的常量名 8. 隐含调用的宏定义 9. 隐含调用的数据结构名 10. 隐含调用的枚举类型名 11. 隐含调用的命令行参数 12. 隐含调用的配置文件名 13. 隐含调用的网络地址 14. 隐含调用的数据库表名 15. 隐含调用的外部API名 16. 隐含调用的系统调用名 17. 隐含调用的GUI组件名 18. 隐含调用的UI界面元素名 19. 隐含调用的web服务端点名 20. 隐含调用的第三方库函数名 特征: 1. 属性名 2. 方法名 3. 类对象名 4. 类型声明 5. 访问修饰符 6. 类型限定符 7. 类型别名 8. 类型大小 9. 类型对齐方式 10. 类型精度 11. 类型范围 12. 类型默认值 13. 类型初始化方式 14. 类型错误处理机制 15. 类型运算符重载方式 16. 类型继承关系 17. 类型多态性 18. 类型封装性 19. 类型抽象性 20. 类型安全性 现象: 1. 输入参数 2. 返回参数 3. 打印信息 4. 异常信息 5. 文件输入输出 6. 网络请求参数 7. 数据库查询参数 8. 用户界面输入 9. 用户界面输出 10. 日志记录信息 11. 调试信息 12. 性能指标 13. 错误码 14. 事件触发条件 15. 中断信号 16. 线程同步机制 17. 进程间通信方式 18. 内存分配方式 19. 文件权限设置 20. 时间戳记录 变化: 1. 函数体 2. 算法实现 3. 数据结构 4. 注释内容 5. 变量命名规范 6. 函数调用顺序 7. 算法时间复杂度 8. 算法空间复杂度 9. 数据结构存储方式 10. 数据结构操作接口 11. 数据结构访问方式 12. 数据结构迭代遍历方式 13. 注释风格规范 14. 变量作用域范围 15. 变量生命周期 16. 函数参数传递方式 17. 函数返回值类型 18. 函数异常处理方式 19. 函数递归调用深度 20. 变量数据类型

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我以为自己很帅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值