此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中文網其他相關文章!