如何使用jQuery将JSP文件包含在另一个JSP文件中

问题描述

像这样单击按钮时,我试图包含一个jsp文件

    <script type="text/javascript">

    jQuery(document).ready(function($) {
        $('#RegisterChildPage').on('click',function() {
            $('.content').html("<%@ include file="FamilyManagerView.jsp"%>");

        });
    });
    </script>

我的问题是在这一行$('.content').html(<%@ include file="FamilyManagerView.jsp"%>);中,存在语法错误,我不知道编写它的正确方法是什么。

由于<%@ include file="FamilyManagerView.jsp"%>,未正确包含

<% %>

解决方法

这可能会对您有所帮助。

将DIV添加到包含其他JSP文件的JSP文件中,并且该文件最初是隐藏的 Hidden div element

然后删除隐藏在onlclick函数中的

onclick function

!document.getElementById('testContainer').hidden-隐藏的HTMLElement属性是一个布尔值,它是true并使用!操作员,您可以显示隐藏的DIV。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Main JSP</title>
</head>
<body>
    <div class="row">
        <div class="col-12">
            <div class="row">
                <button class="btn btn-success" onclick="renderElement()">Render Me</button>
            </div>
            <div class="row mt-2" id="testContainer" hidden>
                <!-- Insert your file here -->
                <%@ include file="test.jsp" %>
            </div>
        </div>
    </div>
</body>
    <script>
        const renderElement = () => {
            document.getElementById('testContainer').hidden = !document.getElementById('testContainer').hidden;
        }
    </script>
</html>