【问题标题】:What window.location.href return in page?什么 window.location.href 在页面中返回?
【发布时间】:2021-08-04 13:37:58
【问题描述】:

我使用 window.location.href 作为元素过滤器。但是,当我单击过滤器按钮时,并未显示所有元素。我操作的不显示,直接从数据库返回的有。

首先,按钮是这样的,每个月1个:

        <div class="monthsButton">   
          <button type="button" data-month="0" >2021</button>
          <button type="button" data-month="1" >Janeiro</button>
          <button type="button" data-month="2" >Fevereiro</button>

其次,要将日期用作过滤器,我使用以下内容:

    const month = req.query.month;
    if(month > 0) {
      launch = lancamentos.filter(item => {    
        const data = new Date(item.data);
        return (data.getMonth() + 1) == month 
      })
    } 

最后,要执行操作,按钮配置如下:

const monthsButton = document.querySelectorAll('.monthsButton button')
for (let button of monthsButton) {
  const month = button.dataset.month
  button.onclick = () => {
    window.location.href= `?month=${month}`
  }
}

问题是过滤器按钮识别日期,使用它,但过滤时不显示,日期和数值也不显示。

【问题讨论】:

  • 您可能希望将const month = req.query.month; 设为数字而不是字符串。我仍然不知道你的实际问题是什么。从你的问题很难确定你的问题是什么。究竟是什么不正常?
  • 返回一个数字。
  • 返回一个字符串格式的数字。我也不知道你的问题是什么。
  • 好的,我修改它返回一个数字,但错误仍然存​​在
  • 我能问一下为什么你得到一个日期对象,把它改成一个字符串,把它分成几部分,再做一个新的日期?为什么你不只是使用第一个日期对象的 getMonth ??? data.getMonth() 应该与 LaunchUtils.formatDate(dataString).getMonth() 没有区别

标签: javascript node.js ejs window.location


【解决方案1】:
Run on you local, because use window.location ...

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<div class="monthsButton">
    <button type="button" data-month="7" >Jul</button>
    <button type="button" data-month="8" >Aug</button>
</div>
<br>

<script>
    const monthsButton = document.querySelectorAll('.monthsButton button')
    for (let button of monthsButton) {
        const month = button.dataset.month
        button.onclick = () => {
            window.location.href= `?month=${month}`
        }
    }

    const lancamentos = [
        {
            name: 'Foo',
            date: 'Wed Aug 04 2021 18:18:14 GMT+0300 (GMT+03:00)' // Month 07
        },
        {
            name: 'Bar',
            date: 'Wed Jul 04 2021 18:18:14 GMT+0300 (GMT+03:00)' // Month 06
        },
        {
            name: 'Bar',
            date: 'Wed Aug 03 2021 18:18:14 GMT+0300 (GMT+03:00)' // Month 07
        }
    ]
    let launch = lancamentos
    const month = +window.location.search[7];
    console.log('Current month = ' , month)
    if(month > 0) {
        launch = lancamentos.filter(item => {
            const date = new Date(item.date)
            console.log('Item month + 1 = ', date.getMonth() + 1)
            return (date.getMonth() + 1) === month //  6 + 1  !== 8,  7 + 1 === 8
        })
    }

    window.document.write(JSON.stringify(launch))
</script>
</body>
</html>

【讨论】:

  • 感谢您的回答。建议提供一些代码解释帮助其他人理解相同
猜你喜欢
  • 2019-12-20
  • 2020-01-03
  • 2016-11-13
  • 2013-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多