此jQuery代码在新的弹出窗口中打开“弹出窗口”的链接,以防止它们在当前页面或新选项卡中打开。 根据需要自定义height
和width
参数。
这是代码:
jQuery(document).ready(function($) { jQuery('a.popup').on('click', function(e) { e.preventDefault(); // Prevent default link behavior const href = $(this).attr('href'); const newwindow = window.open(href, '', 'height=200,width=150'); if (newwindow && newwindow.focus) { newwindow.focus(); } }); });
这个改进的版本使用on
而不是live
(已弃用)以进行更好的事件处理,并包括e.preventDefault()
>可靠地防止默认链接操作。 检查newwindow
添加了鲁棒性。
以上是jQuery在弹出窗口中干净打开链接的详细内容。更多信息请关注PHP中文网其他相关文章!