Skip to content

DOM 94期总265期-浏览器标签页使用JS关闭 #215

@zhangxinxu

Description

@zhangxinxu

补全下面的代码,实现点击按钮关闭浏览器当前的标签页。

<button id="button">关闭</button>

<script>
    button.onclick = () => {
        // 你的代码这里
    };
</script>

顺便补充下代码生效的条件(如果有)。


参考答案
window.close();

条件: 1. 必须在js事件中调用,而不是手动关闭标签页;2. 必须用js方法打开的新页面下调用该方法。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions