我需要在新窗口中打开一组链接 - 好处是它们都具有相同的 css 样式 - 我需要在 css 中做什么才能在新窗口中打开这些链接?
最佳答案
根据评论:
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?
您不能使用 CSS 或 HTML 来执行此操作。您需要使用JavaScript的window.open()
。您可以通过element.getElementsByTagName()
获取所有链接在 a
上,您可以通过 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;
}
}
}
}
或者,如果您已经在使用 jQuery ,您可以使用 $('a.someClass')
来选择具有指定类 someClass
的所有链接:
$(document).ready(function() {
$('a.someClass').click(function() {
window.open(this.href, 'chooseYourName', 'width=600,height=400');
return false;
});
});
chooseYourName
中指定的窗口名称将确保所有链接都在同一窗口中(重新)打开。您还可以看到可以在那里指定宽度和高度。
关于javascript - 如何使用 css 在新窗口中打开链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2796477/