• 前端事件案例补充


    目录

    定时器示例

    搜索框示例

    省市联动


    定时器示例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. <meta name="viewport" content="width=device-width, initial-scale=1">
    7. head>
    8. <body>
    9. <input type="text" id="i1">
    10. <button id="b1">开始button>
    11. <button id="b2">结束button>
    12. <script>
    13. var t;
    14. function showTime() {
    15. var i1Ele = document.getElementById('i1');
    16. var time = new Date();
    17. i1Ele.value = time.toLocaleString();
    18. }
    19. showTime();
    20. var b1Ele = document.getElementById('b1');
    21. b1Ele.onclick = function (ev) {
    22. if (!t){
    23. t = setInterval(showTime,1000)
    24. }
    25. };
    26. var b2Ele = document.getElementById('b2');
    27. b2Ele.onclick = function (ev) {
    28. clearInterval(t);
    29. t = undefined
    30. };
    31. script>
    32. body>
    33. html>

    搜索框示例

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>搜索框示例title>
    6. head>
    7. <body>
    8. <input id="d1" type="text" username="aa" placeholder="请输入关键字" onblur=
    9. "blur1()" onfocus="focus1()">
    10. <script>
    11. function focus1() {
    12. var inputEle = document.getElementById("d1");
    13. var placehold=inputEle.getAttribute('placeholder');
    14. console.log(placehold);
    15. // if (inputEle.value === "请输入关键字") {
    16. if (placehold === "请输入关键字") {
    17. inputEle.value = "";
    18. }
    19. }
    20. function blur1() {
    21. var inputEle = document.getElementById("d1");
    22. // var val = inputEle.value;
    23. var val=inputEle.getAttribute('placeholder');
    24. if (!val.trim()) {
    25. // inputEle.value = "请输入关键字";
    26. inputEle.setAttribute('placeholder', '请输入关键字111');
    27. // 如何去修改标签的属性
    28. }
    29. }
    30. script>
    31. body>
    32. html>

    省市联动

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="x-ua-compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1">
    7. <title>select联动title>
    8. head>
    9. <body>
    10. <select name="" id="province">
    11. <option value="">请选择option>
    12. select>
    13. <select name="" id="city">
    14. <option value="">请选择option>
    15. select>
    16. <script>
    17. var data = {
    18. "河北省": ["廊坊", "邯郸", "保定", "石家庄"],
    19. "北京": ["朝阳区", "海淀区", "大兴区", "通州区"],
    20. "河南省": ["郑州", "周口", "信阳", "洛阳"],
    21. "江苏省": ["南京", "苏州", "徐州", "无锡"],
    22. "浙江省": ["杭州", "绍兴", "温州", "嘉兴"],
    23. "上海": ["黄浦区", "普陀区", "闵行区", "浦东新区"],
    24. };
    25. var province = document.getElementById('province');
    26. var city = document.getElementById('city');
    27. // 1. 要循环data数据,拿到data数据的key值是省作为第一个下拉框的数据
    28. for (var i in data) {
    29. // 这里的i值就是data 的key值,其实就是省数据
    30. // console.log(data[i])
    31. // 2. 创建option标签,怎么样创建标签
    32. var option = document.createElement('option');
    33. // 3. 给option标签设置value值和文本数据
    34. option.value = i; //
    35. option.innerText = i; //
    36. // 4. 把option追加到省的select中取
    37. province.appendChild(option);
    38. }
    39. // 接下来给省的下拉框绑定事件
    40. province.onchange = function () {
    41. // 1. 先拿到当前用户选择的是哪个省的数据
    42. // var current_province = province.value;
    43. var current_province = this.value;
    44. // 2. 根据选择的省获取对应的市信息
    45. var current_city_list = data[current_province];
    46. // ["朝阳区", "海淀区", "大兴", "通州"],
    47. city.innerText = '';
    48. // 3. 循环的把获取到的所有市列表放到第二个select框去
    49. for (var i = 0; i < current_city_list.length; i++) {
    50. var option = document.createElement('option');
    51. //
    52. // 4. 给city标签设置value值和文本数据
    53. option.value = current_city_list[i];
    54. option.innerText = current_city_list[i];
    55. // 5. 把city追加到省的select中取
    56. city.appendChild(option);
    57. }
    58. }
    59. script>
    60. body>
    61. html>
  • 相关阅读:
    PyTorch1.12 亮点一览 | DataPipe + TorchArrow 新的数据加载与处理范式
    基于目录的ant任务
    【1024 | 程序员节】浅谈前端开发中常用的设计模式——适配器模式、工厂模式、单例模式等
    网络安全(黑客)自学
    java设计模式2,开闭原则
    第33章_瑞萨MCU零基础入门系列教程之DHT11温湿度获取实验
    数据结构学习笔记——基数排序和排序算法总结
    ShareSDK 第三方平台分享参数说明
    【经验分享】Wubuntu------体验Windows和Ubuntu的结合体
    高性能业务表结构设计和索引知识深化
  • 原文地址:https://blog.csdn.net/qq_53842456/article/details/134203046