代码之家  ›  专栏  ›  技术社区  ›  Engine

使用nodejs评估复选框位置

  •  0
  • Engine  · 技术社区  · 6 年前

    **index.html**基于W3Schools:

        <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
    }
    
    .switch input { 
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked + .slider {
      background-color: #2196F3;
    }
    
    input:focus + .slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked + .slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    /* Rounded sliders */
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    </style>
    </head>
    <body>
    
    <h2>Toggle Switch</h2>
    
    
    <label class="switch">
      <form id= "test" method="POST" action="test">
      <input type="checkbox" checked>
      <span class="slider round"></span>
    </label>
    </form>
    
    </body>
    </html> 
    

    以及节点应用程序:

    应用程序.js

        const express = require('express');
    const app  = express()
    var path = require('path')
    
    
    console.log(__dirname)
    
    app.get('/',function(req,res){
      res.sendFile(path.join(__dirname+'/index.html'))
    })
    
    
    app.post('/test', (req, res)=>{
        console.log("toggele active ")
      });
    app.listen(3000)
    

    0 回复  |  直到 6 年前
        1
  •  1
  •   Sami Shames El-Deen    6 年前

    您的问题是您的表单没有提交调用,您可以将onClick属性添加到checkbox input的输入中以提交表单:

    <input type="checkbox"
       name='switch'
       onClick="document.forms['test'].submit();"
       checked>
    

    注意:别忘了给输入一个名称,以确定后端中的复选框状态(node.js)

        2
  •  1
  •   Ivan Kolyhalov    6 年前

    所以如果我正确理解了你的问题,你就有了一个类,你想通过两种方式来获取/设置它的属性。如果是这样,我建议在HTML中使用数据属性,您可以通过替换in file在nodejs中设置它们(因为您使用原始HTML),然后获取它们并通过DOM中的js返回node。

    (<div class="whatever" data-somewhat-position='-POSITION WHATEVER-'>) 换成这样:

    let newPosition = // new position here
    fs.readFile('file.html',(err,data)=>{String(data).replace('-POSITION WHATEVER-',newPosition)) }
    

    这将用集合数据呈现html,您可以在DOM中提取这些数据并对其进行操作(即更改类的样式)。

    如何提取数据,答案如下: get data attributes in JavaScript code

    推荐文章