我创建了使用内联javascript在新窗口(而非制表符)中打开的链接:
onclick="window.open(this.href, 'newwindow', 'width=370, height=280'); return false;"
现在我想将这个javascript移动到一个可重用的类。为什么以下不起作用?
$(document).ready(function() {
$('.open-new-window').click(function(){
return false;
window.open(this.href, 'newwindow', 'width=500, height=150');
});
});
答案 0 :(得分:0)
函数在window.open()
函数之前返回,因此返回false;应该在底部
$(document).ready(function() {
$('.open-new-window').click(function(){
window.open(this.href, 'newwindow', 'width=500, height=150');
return false;
});
});
答案 1 :(得分:0)
您的函数在执行window
open
方法之前返回。
答案 2 :(得分:0)
当你从点击处理程序的内联中复制代码时,顺序非常重要。返回将停止该函数并返回一条信息。一旦运行就没有返回任何内容。正确的代码是:
$(document).ready(function() {
$('.open-new-window').click(function(){
window.open(this.href, 'newwindow', 'width=500, height=150');
return false;
});
});
如果您想先阻止点击,可以改为:
$(document).ready(function() {
$('.open-new-window').click(function(e){
e.preventDefault();
window.open(this.href, 'newwindow', 'width=500, height=150');
});
});
第一种方法可能会出现弃用错误(event.returnValue is deprecated. Please use the standard event.preventDefault() instead)