我如何重写以下内容,以便我也可以声明"left"one_answers"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);
});