会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133333个问题
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 408楼
WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 411楼
WEB前端全系列/第二阶段:JavaScript编程模块/字符串与数组 412楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 413楼
WEB前端全系列/第二阶段:JavaScript编程模块/浏览器模型(BOM) 414楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    li{background-color: skyblue}
    li:hover{
        background-color: #ff6700;
    }
</style>
<body>
    <button id="button1">获取元素</button>
    <button id="button2">设置元素</button>
    <button id="button3">删除元素</button>

<script>
    var div1=document.createElement('div');
    div1.style.cssText='width:100%;position:relative;background:red;';
    var div2=document.createElement('div');
    div2.style.cssText='width:80%;position: absolute;left: 50%;transform: translateX(-50%)';
    document.body.appendChild(div1);
    var arr=['首页','军事','科技','新闻','游戏'];
    var ul=document.createElement('ul');
    ul.style.cssText='margin:0;padding:0;list-style;text-align: center;background:pink;';
    for (var i=0;i<arr.length;i++){
        var li=document.createElement('li');
        li.style.cssText='display: inline-block;  width: 100px;height: 30px;  line-height: 30px;\
                        text-align: center;  margin-left:5px';
        var a=document.createElement('a');
        a.style.cssText=' text-decoration: none;color: white;';
        a.innerHTML=arr[i];
        li.appendChild(a);
        ul.appendChild(li);
    }
    div2.appendChild(ul);
    div1.appendChild(div2);
    document.body.appendChild(div1);

    var button=document.querySelector('div');
    document.querySelector('#button1').onclick=function () {
       console.log( button.getAttribute('style'));
    };
    document.querySelector('#button2').onclick=function () {
        button.setAttribute('style','display:none');
    };
    document.querySelector('#button3').onclick=function () {
        button.removeAttribute('style');
    }
</script>

</body>
</html>

老师为啥我div1颜色设置不出来

WEB前端全系列/第二阶段:JavaScript编程模块/DOM模型 415楼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  账号<input type="text">
  密码<input type="text">
  <button>提交</button>
  <script>
    let userName = document.getElementsByTagName("input")[0];
    let passWord = document.getElementsByTagName("input")[1];
    let btn = document.getElementsByTagName("button")[0];

    btn.onclick = () => {
      let xhr = new XMLHttpRequest();//创建xhr对象通过XMLHttpRequest构造函数
      
      xhr.onreadystatechange = () => {
        if( xhr.readyState == 4 && xhr.status == 200 ){
          //进了这里证明拿到了数据
          console.log(xhr.responseText);
        }
      }

      let datas = new FormData();
      datas.append("userName",userName.value);
      datas.append("passWord",passWord.value);

      xhr.open("post","http://localhost:3000/index",true);
      
      xhr.send(datas);
    }
    
  </script>
  
</body>
</html>
let express = require("express");
let bodyParser = require("body-parser");
//let multiparty = require("multiparty"); /*引入multiparty模块*/
//需要安装 npm install multiparty --save
let app = express();

app.use(bodyParser.urlencoded({ extended: false }));

app.all("*", function (req, res, next) {
  //解决夸域问题
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", "3.2.1");
  res.header("Content-Type", "application/json;charset=utf-8");
  next();
});

// app.post("/index", (req, res) => {
//   console.log(req.body);
//   res.send("后台接收成功");
// });
app.post("/index", function (req, res) {
  // let form = new multiparty.Form();
  // form.parse(req, function (err, fields, file) {
  //   console.log(fields);
  //   res.send("数据已接收");
  // });
  console.log(req.body);
  res.send("接收成功!");
});

app.listen(3000, () => {
  console.log("3000链接成功");
});

后端拿不到前端传输过来的数据 老师 帮忙看看

WEB前端全系列/第二阶段:JavaScript编程模块/运算符_数据类型和流程循环语句 416楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637