如何重写以下内容,以便我还可以声明“left”和“top”参数,以便使弹出窗口居中?
$('.open-new-window').click(function(){
var window_width = $(this).data("window-width"),
window_height = $(this).data("window-height"),
window_name = $(this).data("name");
window.open(this.href, window_name, 'width=' + window_width + ', height=' + window_height);
return false;
});
我尝试过很多变体:
$('.open-new-window').click(function(){
var window_width = $(this).data("window-width"),
window_height = $(this).data("window-height"),
window_name = $(this).data("name");
window.open(this.href, window_name, left='100px', top='100px, 'width=' + window_width + ', height=' + window_height);
return false;
});
最佳答案
试试这个(字符串内的设置):
var settings = 'width=300, height=500, top=20, left=20, scrollbars=yes, location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=yes, dependent=no';
var win = window.open(popupUrl, popupTitle, settings);
这是完整的示例:
<a class="open-new-window" data-window-width="300" data-window-height="350" data-window-url="path_to_file.html" data-window-top="30" data-window-left="30" data-window-title="My Window" href="path_to_file.html">Open Popup</a>
$('.open-new-window').click(function(e){
e.preventDefault();
var thisLink = $(this);
var windowUrl = thisLink.data("windowUrl");
var windowWidth = thisLink.data("windowWidth"),
var windowHeight = thisLink.data("windowHeight"),
var windowTitle = thisLink.data("windowTitle");
var windowTop = thisLink.data("windowTitle");
var windowLeft = thisLink.data("windowTitle");
var settings = 'width=' + windowWidth + ', height=' + windowHeight + ', top=' + windowTop + ', left=' + windowLeft + ', scrollbars=yes, location=no, directories=no, status=no, menubar=no, toolbar=no, resizable=yes, dependent=no';
window.open(windowUrl, windowTitle, settings);
});
关于javascript - 如何将 left 和 top 参数添加到在新窗口中打开的 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26847398/