使用 window.open() (最简单直接)

这种方法会在新的浏览器标签页或窗口中打开指定的网页。

jquery点击图片弹出网页代码
(图片来源网络,侵删)

实现思路

  1. 给图片添加一个 idclass 以便 jQuery 选中它。
  2. 编写 jQuery 代码,监听该图片的 click 事件。
  3. 在事件处理函数中,使用 window.open() 方法打开目标 URL。

完整代码示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">jQuery 点击图片弹出网页</title>
    <!-- 1. 引入 jQuery 库 -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        body {
            font-family: sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        .clickable-image {
            cursor: pointer; /* 鼠标悬停时显示手型光标 */
            border: 2px solid #ddd;
            border-radius: 8px;
            transition: transform 0.2s;
        }
        .clickable-image:hover {
            transform: scale(1.05); /* 鼠标悬停时轻微放大 */
            border-color: #007bff;
        }
        p {
            margin-top: 20px;
            color: #666;
        }
    </style>
</head>
<body>
    <h1>点击下方图片</h1>
    <!-- 给图片添加一个 class -->
    <img src="https://via.placeholder.com/300x200/007bff/ffffff?text=点击我" alt="可点击的图片" class="clickable-image">
    <p>这将在新标签页中打开 jQuery 官方网站。</p>
    <!-- 2. 编写 jQuery 代码 -->
    <script>
    $(document).ready(function(){
        // 当 class 为 clickable-image 的元素被点击时
        $('.clickable-image').on('click', function(){
            // window.open() 打开新窗口
            // '_blank' 是一个特殊的目标,表示在新标签页或窗口中打开
            window.open('https://jquery.com', '_blank');
        });
    });
    </script>
</body>
</html>

使用 Bootstrap Modal (最专业、用户体验最好)

这种方法会弹出一个模态框(Modal),在当前页面内显示一个网页的内容,这通常需要加载外部网页的内容,这里我们使用一个在线的 HTML 文件作为示例。

准备工作: 您需要一个模态框的 HTML 结构,Bootstrap 提供了非常方便的模态框组件。

实现思路

  1. 引入 Bootstrap CSS 和 JS,以及 jQuery。
  2. 在 HTML 中创建 Bootstrap 模态框的结构(包括遮罩层、弹窗内容等)。
  3. 给图片添加点击事件。
  4. 在点击事件中,使用 jQuery 的 load() 方法动态地将目标网页的内容加载到模态框中,然后显示模态框。

完整代码示例

创建一个简单的目标网页 content.html (与主页面在同一目录下):

content.html

jquery点击图片弹出网页代码
(图片来源网络,侵删)
<!DOCTYPE html>
<html>
<head>弹出的内容页面</title>
    <style>
        body { font-family: sans-serif; padding: 20px; color: #333; }
        h2 { color: #007bff; }
    </style>
</head>
<body>
    <h2>这是从另一个页面加载的内容!</h2>
    <p>你好,欢迎来到这个弹出的模态框窗口,这里的内容是通过 jQuery 的 load() 方法动态加载进来的。</p>
    <p>你可以在这里放任何 HTML 内容,比如图片、文本、表单等。</p>
</body>
</html>

主页面 index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">jQuery 点击图片弹出模态框</title>
    <!-- 1. 引入 Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入 jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 引入 Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        body {
            font-family: sans-serif;
            text-align: center;
            padding-top: 50px;
        }
        .clickable-image {
            cursor: pointer;
            border: 2px solid #ddd;
            border-radius: 8px;
            transition: transform 0.2s;
        }
        .clickable-image:hover {
            transform: scale(1.05);
            border-color: #0d6efd;
        }
    </style>
</head>
<body>
    <h1>点击图片弹出模态框</h1>
    <!-- 点击的图片 -->
    <img src="https://via.placeholder.com/300x200/0d6efd/ffffff?text=点击弹出内容" alt="点击弹出内容" class="clickable-image" id="openModalBtn">
    <!-- 2. Bootstrap 模态框结构 -->
    <div class="modal fade" id="contentModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalLabel">加载的内容</h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          </div>
          <div class="modal-body" id="modal-body-content">
            <!-- 内容将通过 jQuery 动态加载到这里 -->
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
          </div>
        </div>
      </div>
    </div>
    <!-- 3. 编写 jQuery 代码 -->
    <script>
    $(document).ready(function(){
        // 当 id 为 openModalBtn 的图片被点击时
        $('#openModalBtn').on('click', function(){
            // 清空模态框旧内容,避免重复加载
            $('#modal-body-content').html('<p>加载中...</p>');
            // 使用 jQuery 的 load() 方法加载外部 HTML 文件的内容
            // 并将加载到的内容放入 id 为 modal-body-content 的 div 中
            $('#modal-body-content').load('content.html', function(response, status, xhr) {
                if (status == "error") {
                    $(this).html("抱歉,加载内容时出错。");
                    console.error("Error loading page: " + xhr.status + " " + xhr.statusText);
                } else {
                    // 内容加载成功后,显示模态框
                    // 创建一个新的 Bootstrap Modal 实例并显示
                    var myModal = new bootstrap.Modal(document.getElementById('contentModal'));
                    myModal.show();
                }
            });
        });
    });
    </script>
</body>
</html>

总结与对比

特性 方法一: window.open() 方法二: Bootstrap Modal
实现难度 非常简单,几行代码即可 稍复杂,需要理解 Bootstrap 结构和 jQuery load()
用户体验 跳转到新页面,用户可能会迷失 体验流畅,内容在当前页面弹出,不易迷失
适用场景 打开外部链接、官网等独立页面 展示来自本站其他页面的内容、图片集、表单等
依赖库 仅需 jQuery 需要 jQuery 和 Bootstrap CSS/JS

如何选择?

  • 如果只是想让用户跳转到另一个网站,方法一最快最简单。
  • 如果你想在当前页面内优雅地展示一段来自其他页面的内容,或者创建一个图片画廊、详情弹窗等,方法二是专业且更好的选择。
jquery点击图片弹出网页代码
(图片来源网络,侵删)