JavaScript---表单验证和jQery

1.表单验证

表单是什么 form DOM 树

  • 文本框 text
  • 下拉框 < select >
  • 单选框 radio
  • 多选框 checkbox
  • 隐藏域 hidden
  • 密码框 password
  • …….

表单的目的:提交信息

获得要提交的信息

<form action="post">
    <p>
        <span>用户名:</span> <input type="text" id="username">
    </p>

    <!--多选框的值,就是定义好的value -->
    <p>
        <span>性别:</span>
        <input type="radio" name="sex" value="man" id="boy"><input type="radio" name="sex" value="women" id="girl"></p>

</form>

<script>
    var input_text = document.getElementById('username');
    var boy_radio = document.getElementById('boy');
    var girl_radio = document.getElementById('girl');

    // 得到输入框的值
    input_text.value
    // 修改输入框的值
    input_text.value = '123'


    // 对于单选框,多选框等等固定的值,boy_radio.value只能取到当前的值
    boy_radio.checked; //查看返回的结果,是否为true,如果为true,则被选中~
    girl_radio.checked = true; //赋值

</script>

提交表单。md5 加密密码,表单优化

<!--
表单绑定提交事件
οnsubmit= 绑定一个提交检测的函数, true, false
将这个结果返回给表单,使用 onsubmit 接收!
οnsubmit="return aaa()"
-->
<form action="https://www.baidu.com/" method="post" onsubmit="return aaa()">
    <p>
        <span>用户名:</span> <input type="text" id="username" name="username">
    </p>
    <p>
        <span>密码:</span> <input type="password" id="input-password">
    </p>

    <input type="hidden" id="md5-password" name="password">

    <!--绑定事件 onclick 被点击-->
    <button type="submit">提交</button>
</form>


<script>
    function aaa() {
        alert(1);
        var uname = document.getElementById('username');
        var pwd = document.getElementById('input-password');
        var md5pwd = document.getElementById('md5-password');

        md5pwd.value = md5(pwd.value);
        // 可以校验判断表单内容,true就是通过提交,false,阻止提交
        return true;
    }
</script>

2.jQery

JavaScript

jQuery库,里面存在大量的Javascript函数

获取jQuery

在这里插入图片描述
公式 :$(选择器).事件(事件函数)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--<script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>-->
    <script src="lib/jquery-3.4.1.js"></script>
</head>
<body>

<a href="" id="test-jquery">点我</a>

<script>

    //选择器就是css的选贼器
    $('#test-jquery').click(function () {
        alert('hello,jQuery');
    })

</script>

</body>
</html>

选择器

    //原生js,选择器少,麻烦不好记
    //标签
    document.getElementsByTagName();
    //id
    document.getElementById();
    //类
    document.getElementsByClassName();

    //jQuery  css 中的选择器它全部都能用!
    $('p').click(); //标签选择器
    $('#id1').click(); //id选择器
    $('.class1').click() //class选择器

文档工具站:http://jquery.cuishifeng.cn/

事件

鼠标事件,键盘事件,其他事件
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="lib/jquery-3.4.1.js"></script>
    <style>
        #divMove{
            width: 500px;
            height: 500px;
            border: 1px solid red;
        }
    </style>
</head>
<body>

<!--要求:获取鼠标当前的一个坐标-->
mouse :<span id="mouseMove"></span>
<div id="divMove">
在这里移动鼠标试试
</div>

<script>
    //当网页元素加载完毕之后,响应事件
    $(function () {
        $('#divMove').mousemove(function (e) {
            $('#mouseMove').text('x:'+e.pageX + 'y:'+e.pageY)
        })
    });
</script>


</body>
</html>

操作DOM

节点文本操作

$('#test-ul li[name=python]').text(); //获得值
$('#test-ul li[name=python]').text('设置值'); //设置值
$('#test-ul').html(); //获得值
$('#test-ul').html('<strong>123</strong>'); //设置值

css的操作

 $('#test-ul li[name=python]').css({"color","red"})

元素的显示和隐藏: 本质 display : none

$('#test-ul li[name=python]').show()
$('#test-ul li[name=python]').hide()

娱乐测试

$(window).width()
$(window).height()
$('#test-ul li[name=python]').toggle();

未来ajax();

$('#from').ajax()

$.ajax({ url: "test.html", context: document.body, success: function(){
    $(this).addClass("done");
}});

小技巧

1、如果巩固JS ( 看jQuery 源码, 看游戏源码!)

2、巩固HTML。CSS (扒网站,全部down下来,然后对应修改看效果~)

Layer 弹窗组件

Element-ui

发布了39 篇原创文章 · 获赞 1 · 访问量 533

猜你喜欢

转载自blog.csdn.net/love_to_share/article/details/103933484
今日推荐