关闭按钮上的窗口或选项卡 [英] Close windows or tabs on a button click

查看:78
本文介绍了关闭按钮上的窗口或选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在运行我的网站的浏览器中打开了一个选项卡.点击按钮后,我的网站将打开其他窗口或标签,每个窗口或标签中的URL都不相同.

I have a tab open in my browser which runs my web site. Upon clicking on a button, my site is going to open other windows or tabs with different URLs in each.

<form action="search.php" method="get">
    <input type="text" name="item" size="30" style="font-size:140%" id="item">
    <input type="submit" value="Search All" style="font-size:140%" id="submit">
</form>

执行此操作的JS提供如下:

The JS that performs this is provided below:

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>

$(document).ready(function(){
    function get(id) {
        return document.getElementById(id);
    }
    $("#submit").click(function() {
        get("guitarcentertext").value = get("item").value;
        get("music123text").value = get("item").value;
        get("musiciansfriendtext").value = get("item").value;
        get("prosoundtext").value = get("item").value;

        $("input#guitarcenterbutton").click();
        $("input#btnSearch").click();
        $("input#musiciansfriendbutton").click();
        $("input#prosoundbutton").click();
        return false;
    });
});
</script>

问题:是否可以通过单击另一个按钮来关闭以前打开的选项卡?

Question: Is there a way that upon the click of another button, I can close the tabs that I previously opened?

感谢您的帮助! :)

推荐答案

您可以使用 window.open()来创建新窗口,并控制何时关闭它们.

You can use window.open() to create new windows, and to control when to close them.

示例:

function mypopup() {
    mywindow = window.open ("http://www.javascript-coder.com",
                            "mywindow",
                            "location=1,status=1,scrollbars=1,"+
                            "width=100,height=100");
    mywindow.moveTo(0,0); // move the window to a particular location

    // do your stuff ...

    mywindow.close(); // close that popup when you are done
}

如您所见,您可以使用 window.close().

As you can see, you can control when you want to close the windows using window.close().

您只需在链接中使用target="_blank"即可在新选项卡中打开链接,但是您说您有一个表单,其提交将导致打开新窗口.但是,与其使用提交按钮,不如使用普通按钮并将事件处理程序附加到被单击的事件处理程序上.单击它后,打开窗口并使用您的mojo.

You can have links open in new tabs simply by using target="_blank" in the link, but you say that you have a form whose submission will cause a new window to be opened. But instead of using a submit button, use a plain button and attach an event handler to it being clicked. When it is clicked, open your window and work your mojo.

关于您关于打开选项卡而不是窗口的查询,我不确定是否可以完成.我可以想到使用服务器端参与的几种不良方法,但是我敢肯定您不会在寻找这种方法.甚至使用target="_blank"只是告诉浏览器不要在同一页面中打开链接.由浏览器决定是要选择一个新窗口还是一个新选项卡(通常是一个新选项卡).

Regarding your query about opening tabs instead of windows, I'm not entirely sure it can be done. I can think of a couple of bad ways to do it using server side participation, but I'm pretty sure you are not looking for that. Even using target="_blank" just tells the browser not to open the link in the same page. Its upto the browser to choose a new window or a new tab (mostly, its a new tab).

干杯!

这篇关于关闭按钮上的窗口或选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆