JS 需要上网查的语句

系列文章目录


前言

随着时代的进步和发展,一堆vip和广告入侵人们的生活,使本该运用到学习的时间,却耗费在寻找知识的途中,本来一分钟能解决的时花了几十分钟。所以我决定在此写下属于我的笔记。并且需要告诫自己在学习的途中,也要把知识给记录下来。


一、字符串相关

cookie

浏览器cookie
设置cookie:生命周期:页面关闭消失,刷新存在;
document.cookie = “numpage=” + numpage;
获得cookie
var cookie = document.cookie;
本地cookie:关闭浏览器,重新打开照样在;
设置:localStorage.setItem(“numpage”, numpage);

获得:var numpage = localStorage.getItem(“numpage”);

时间戳转日期,yyyy:mm:dd

 var datetime = new Date(parseInt(time));
 datetime = datetime.getFullYear() + "-" + (datetime.getMonth() + 1) + "-" + parseInt(datetime.getUTCDate()+1);
  // datetime.getFullYear();  // 获取完整的年份(4位,1970)
  // datetime.getMonth();  // 获取月份(0-11,0代表1月,用的时候记得加上1)
  // datetime.getDate();//获取天数
  // datetime.getDay();//获取周几

二、ajax

ajax-post请求

//点击评论按钮发送Post请求
$(".comment_commit").click(function () {
    let pl_text ={
        com_userNo:"",
        a_id:"",
        parentID:"",
        comments:"",
        grandpaID:""
    };
    var ul_name = "."+($(this).closest("ul").attr("class"));
    var li_name = "."+$(this).closest("li").attr("class");
    pl_text.com_userNo=userNo;
    pl_text.a_id=a_id;
    pl_text.parentID=$(ul_name+" "+li_name).attr("data-commentid");
    pl_text.comments=$(ul_name+" "+li_name+" .textarea_comment").val();
    if (ul_name == ".undefined"){
        ul_name=".wenzhang_pinglun";
        pl_text.comments=$(ul_name+" .textarea_comment").val();
    }
    pl_text.grandpaID=$(ul_name+" .zhu_pinglun").attr("data-commentid");
    if (pl_text.comments = undefined || pl_text.comments == ""){return}
    $.ajax({
        type: "POST",
        url: baseUrl+"/index/addComment.do",
        dataType:"json",
        contentType:"application/json;charset=utf-8",
        data:JSON.stringify(pl_text),
        success: function(data){
            // alert(data);
            // location.reload();
            history.go(0);//刷新当前页面
        }
    });
});

后端

//发表评论
    @ResponseBody
    @RequestMapping("/addComment.do")
    public boolean addComments(@RequestBody Map<String, Object> map) {
        int i = article_commentsService.addComment(map);
        boolean flag = false;
        if (i == 1) flag = true;
        return flag;
    }

ajax-get请求

$(function () {
        $.ajax({
            "url":"/hmbb2_war_exploded/index/list_login",
            "type":"get",
            "dataType":"json",
            "success":function(datao){

                for(var i = 0; i < datao.length; i++){
                    var html2 = "";
                    var num = i+1;
                    html2+='  <div class="text-left" class="divbody" style="margin-left: 288px;">\n' +
                        '            <div></div>\n' +
                        '            <div><img src="${pageContext.request.contextPath}/img/hh1.jpg" width="100px" height="100px"> <span><a href="${pageContext.request.contextPath}/index/article?id=${article.a_id}">'+datao[i].a_content+'</a></span></div>\n' +
                        '        </div>'
                    $('#cityAreaId').append(html2);
                }
            },
            "error":function(){
                alert("服务器错误");
            }
        });
    })

页面刷新

自动刷新页面
<//meta http-equiv=“refresh” content=“5”>

<html>
<head>
    <title>${article.a_name}</title>
    <meta http-equiv="refresh" content="5">

手动

// location.reload();//刷新页面最后一个请求
history.go(0);//当前页面啥样,刷新后啥样,用于更新

在外部引用js文件中获得当前项目路径

在html/jsp中

<input id="PageContext" type="hidden" value="${pageContext.request.contextPath}"/>

在外部js文件中

var baseUrl = $("#PageContext").val();

引用

$.ajax({
        "url": baseUrl + "/index/comments?a_id=" + id,
        "type": "get",
        "async": false,
        "dataType": "json",

请求回来的html,js无法生效

需要加上 “async”: false,
async:异步

 "url": baseUrl + "/index/comments?a_id=" + id,
        "type": "get",
        "async": false,
        "dataType": "json",

获取当前请求地址的参数

//请求地址:http://localhost:8080/hmbb2_war_exploded/index/article?id=1
let id = location.search;//id = ?id=1
id = id.substr(id.indexOf("=") + 1);//id = 1

三、 数组有关

如何判断一个值是否在数组中

var arr = [1,2,3];
if(arr_pl.indexOf(parseInt(data[ul_num].id)) != -1){}

-1代表false

四、JSP

forEach遍历文章列表

前端

<c:forEach var="article" items="${articles}">
    <div class="text-left" class="divbody" style="margin-left: 288px;">
        <div></div>
        <div><img src="${pageContext.request.contextPath}/img/hh1.jpg" width="100px" height="100px"> <span><a
                href="${pageContext.request.contextPath}/index/article?id=${article.a_id}">${article.a_name}</a><span
                style="margin-left:600px;position: absolute;left: 400px;line-height: 6">${article.a_date}</span></span>
        </div>
    </div>
</c:forEach>

后端

 @RequestMapping("/list_login")
    public String selectArticleAll_login(Model model, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        model.addAttribute("user",user);
        List<Article> articles = articleService.selectArticleAll();
        model.addAttribute("articles",articles);
        return "index_login";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值