<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>h</title></head><body> 用户名:<input id="name" type="text" name="name"> 密码:<input id="pwd" type="text" name="pwd"> <button id="addBtn">添加</button> <button id="delBtn">删除</button> <div> <input id="search" type="text" placeholder="输入键名查询"> <button id="checkBtn">查看</button> </div> <script> var objName = document.getElementById('name'); var objPwd = document.getElementById('pwd'); var objAddBtn = document.getElementById('addBtn'); var objDelBtn = document.getElementById('delBtn'); var objSearch = document.getElementById('search'); var objCheckBtn = document.getElementById('checkBtn'); function setCookie(name, value) { document.cookie = name + '=' + value; } function getCookie(name) { var n = name + '='; var arr = document.cookie.split(';'); console.log(arr); arr.map(i => { let j = i.trim(); if (j.indexOf(n) === 0) { let res = j.substring(j.length, n.length) console.log(j.substring(j.length, n.length)); return res; } }) } function removeCookie(name) { setCookie(name, '1', '-1'); } addBtn.onclick = function () { const name = objName.value; const pwd = objPwd.value; setCookie(name, pwd); } objCheckBtn.onclick = function () { let ipt = objSearch.value; debugger let a = getCookie(ipt); console.log(a); } </script></body></html>
输入cookie键名,点击按钮查看,但是返回值一直都是undefined,用console.log打印arr和res都是有值的,但是调试的时候就提示undefined,这是为什么?