爱编程的设计师 2022-09-07 11:42 采纳率: 94.4%
浏览 37
已结题

设置了location.href,为什么log打印却报错

我想要达到的结果

```html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .btn{
            width: 100px;
            height: 30px;
        }
    </style>
</head>
<body>
    <button class="btn"></button>
    <script>
        // location.assign = 'https://blog.csdn.net/'
        // console.log(location);
        // console.log(location.href);
        // console.log(location.host);
        // console.log(location.search);
        // console.log(location.hash);
        // console.log(location);
        document.querySelector('.btn').onclick = () => {
            location.href = 'https://blog.csdn.net/'
            console.log(location.href);
        }
    </script>
</body>
</html>

```

  • 写回答

5条回答 默认 最新

  • 狂龙骄子 2022-09-07 14:31
    关注

    location.href 属性,是设置或返回当前显示的文档的完整 URL。

    如果要返回或打印当前页面的URL,要将 console.log(location.href) 放在前面一行。
    预览时,断点打在 location.href = 'https://blog.csdn.net/' 行上;
    否则跳转到新页面后, console 控制页面输出的是新的内容,会覆盖 当前页面的 输出。

    document.querySelector('.btn').onclick = () => {
        // 打印当前文档的URL
        console.log(location.href);
        // 设置新的URL(并跳转)
        location.href = 'https://blog.csdn.net/'
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 9月16日
  • 已采纳回答 9月8日
  • 创建了问题 9月7日

悬赏问题

  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000