简体   繁体   English

如何使用CSS在新窗口中打开链接?

[英]How can I make a link open in a new window using css?

我需要在新窗口中打开一组链接-好处是它们都具有相同的CSS样式-我需要在CSS中做什么才能在新窗口中打开这些链接?

As per the comments: 根据评论:

and how can i specify window size? 以及如何指定窗口大小? i want it a little smaller than the original page. 我想要它比原始页面小一点。 also, can i make all links in the page, open in the SAME new window, of a smaller size? 另外,我可以在页面的所有链接中以较小的尺寸在SAME新窗口中打开吗?

You can't use CSS or HTML to do this. 您不能使用CSS或HTML来执行此操作。 You need to use JavaScript's window.open() . 您需要使用JavaScript的window.open() You can get all links by element.getElementsByTagName() on a and you can determine the link's class attribute by element.className : 您可以通过在上a element.getElementsByTagName()获取所有链接,并可以通过element.className确定链接的class属性:

window.onload = function() {
    var links = document.getElementsByTagName('a');
    for (var i = 0; i < links.length; i++) {
        var link = links[i];
        if (link.className == 'someClass') {
            link.onclick = function() {
                window.open(this.href, 'chooseYourName', 'width=600,height=400');
                return false;
            }
        }
    }  
} 

Or if you're already using jQuery , you can use $('a.someClass') to select all links which has the specified class someClass : 或者,如果您已经在使用jQuery ,则可以使用$('a.someClass')选择具有指定类someClass所有链接:

$(document).ready(function() {
    $('a.someClass').click(function() {
        window.open(this.href, 'chooseYourName', 'width=600,height=400');
        return false;
    });
});

The window's name as specified in chooseYourName will take care that all links are (re)opened in the same window. chooseYourName指定的窗口名称将确保所有链接都在同一窗口中(重新)打开。 You also see that you can specify the width and height over there. 您还将看到可以在那儿指定宽度和高度。

You can't use CSS to do this. 您不能使用CSS来做到这一点。 You need to use <a target="_blank"></a> . 您需要使用<a target="_blank"></a>

Edit: Javascript's window.open command. 编辑: Javascript的window.open命令。

You can't do this with CSS. 您不能使用CSS做到这一点。 You should do this with a small script such as: 您应该使用一个小的脚本来执行此操作,例如:

<script type="text/javascript">
function newwindow()
{
    var load = window.open('http://www.domain.com');
}
</Script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM