这段代码如何转化为jquery代码实现

1.function getuser() {
            var username = document.getElementById("username").value;
            var password = document.getElementById("password").value;
            testing(username, password)
        }

        function testing(username, password) {
            var tmp = username && password;
            if (tmp == "") {
                alert("请填写完整信息");
                
            }else{
                window.location.href = "comment.html";                
                alert("登录成功");
                localStorage.setItem("username",username);
            }
            
        }        

 

 

 

2.var username=document.getElementById("username");
        var txt=document.getElementsByClassName("txt")[0];
        var btn=document.getElementsByTagName("button")[0];
        var pl=document.getElementsByClassName("pl")[0];
        var len=document.getElementById("len");
        if(localStorage.length>0){
            username.value=localStorage.getItem("username");
        }
        txt.oninput=function(){
            len.innerHTML=txt.value.length;
        }
        
        btn.onclick=function(){
            if(username.value=='' || txt.value==''){
                confirm("确定不提交内容吗?");
                return false;
            }else{
                var div=document.createElement("div");
                var span=document.createElement("span");
                var small=document.createElement("small");
                var a=document.createElement("a");
                var p=document.createElement("p");
                span.innerText=username.value;
                small.innerText="\t"+new Date().toLocaleString();
                a.innerText='删除';
                a.style.float="right";
                a.style.href="#";
                p.innerText=txt.value;
                
                div.appendChild(span);
                div.appendChild(small);
                div.appendChild(a);
                div.appendChild(p);
                
                pl.parentNode.appendChild(div)
            }
            a.onclick=function(){
                a.parentNode.remove();
            }
        }

1.function getuser() {
            var username = $("#username").val();
            var password = $("#password").val();
            testing(username, password)
        }

        function testing(username, password) {
            var tmp = username && password;
            if (tmp == "") {
                alert("请填写完整信息");
                
            }else{
                window.location.href = "comment.html";                
                alert("登录成功");
                localStorage.setItem("username",username);
            }
            
        }        

 

 

document.getElementById这种代码转换为$("#ID")

首先明白这段代码实现什么功能,然后去了解jquery的语法,只要会js,学习jquery很快上手

        function getuser() {
            var username = $("#username").val();
            var password = $("#password").val();
            testing(username, password)
        }
        function testing(username, password) {
            var tmp = username && password;
            if (tmp == "") {
                alert("请填写完整信息");
            }else{
                window.location.href = "comment.html";                
                alert("登录成功");
                localStorage.setItem("username",username);
            }
        }        
 
        var username=$("#username");
        var txt=$(".txt");
        var btn=$("button");
        var pl=$(".pl");
        var len=$("#len");
        if(localStorage.length>0){
            username.val(localStorage.getItem("username"));
        }
        txt.on("input",function(){
            len.html(txt.val().length);
        });
        btn.click(function(){
            if(username.val()=='' || txt.val()==''){
                confirm("确定不提交内容吗?");
                return false;
            }else{
                var div=$("<div>");
                var span=$("<span>");
                var small=$("<small>");
                var a=$("<a>");
                var p=$("<p>");
                span.text(username.val());
                small.text("\t"+new Date().toLocaleString());
                a.text('删除');
                a.css("float","right");
                a.attr("href","#");
                p.text(txt.val());
                div.append(span);
                div.append(small);
                div.append(a);
                div.append(p);
                pl.parent().append(div)
            }
            a.click(function(){
                a.parent().remove();
            })
        });

 

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632