我使用onlick来调用具有不同参数的函数,但它并不总是有效。我试图这样做:
<?php if(something...) { ?>
<button type="button" onclick="PopupCenterDual('<?php echo URL::base()?>someurl','title1','800','420'); " href="" >Register</button>
<?php } else { ?>
<button type="button" onclick="PopupCenterDual('<?php echo URL::base()?>anotherurl','title2','800','420'); " href="" >Register</button>
<?php } ?>
<script src="@Url.Content(this.VirtualPath).Replace(".cshtml",".ts")"></script>
当页面完全加载并传递这些参数时如何做到这一点?
答案 0 :(得分:2)
每当您在任何其他函数中定义任何变量或函数时,您只能从该函数范围内访问它。它被称为局部范围变量或函数。在您的情况下,您已将函数PopupCenterDual
放在此闭包函数中,该函数成为此函数的本地函数:
$(document).ready(function() {
// Your local scope function
});
//Move your function here to give it a global scope
但是你的onclick事件是从全局范围调用此函数。因此,您需要将其移到外部以使其全局化,以便您的onclick事件处理程序可以访问它。
答案 1 :(得分:1)
请尝试此
请在(document).ready(function() {
(document).ready(function() {
});
function PopupCenterDual(url, title, w, h) {
var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
var left = ((width / 2) - (w / 2)) + dualScreenLeft;
var top = ((height / 2) - (h / 2)) + dualScreenTop;
var newWindow = window.open(url, title, 'scrollbars=no, resizable=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
if (window.focus) {
newWindow.focus();
}
}