首頁 >web前端 >js教程 >jQuery在彈出窗口中乾淨打開鏈接

jQuery在彈出窗口中乾淨打開鏈接

Joseph Gordon-Levitt
Joseph Gordon-Levitt原創
2025-03-05 00:34:08333瀏覽

此jQuery代碼在新的彈出窗口中打開“彈出窗口”的鏈接,以防止它們在當前頁面或新選項卡中打開。 根據需要自定義heightwidth參數。

>

jQuery Cleanly Open Links in Popup Windows

這是代碼:

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn