为什么我的javascript代码在wordpress的HTML块编辑器中不起作用

问题描述 投票:0回答:1

我已经尝试运行此代码两个小时,并且总是遇到相同的错误,即没有输出,但是当我在发布之前预览它时,一切都很好并且工作正常。我正在为我的 WordPress 网站使用生成主题插件,我提供了下面的代码。

代码:

<!DOCTYPE html>
<html>
<head>
    <title>Average Grade Calculator</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 0;
        }

        h1 {
            text-align: center;
            color: #333;
        }

        #inputForm {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.2);
            border-radius: 10px;
            border: 2px solid #333;
        }

        table {
            width: 100%;
            margin-bottom: 10px;
            border-collapse: collapse;
        }

        th, td {
            padding: 10px;
            text-align: center;
        }

        th {
            background-color: #333;
            color: #fff;
        }

        input[type="number"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
        }

        button {
            display: block;
            margin: 0 auto;
            background-color: #333;
            color: #fff;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #555;
        }

        #resultBox {
            border: 2px solid #333;
            padding: 10px;
            text-align: center;
            margin-top: 20px;
            border-radius: 5px;
        }
    </style>
</head>
<body>
    <h1>Average Grade Calculator</h1>

    <form id="inputForm">
        <table>
            <tr>
                <th>Grade</th>
                <th>Weight</th>
            </tr>
            <!-- Input fields for grade and weight -->
            <tr>
                <td><input type="number" id="grade1" step="0.01" placeholder="Assignment Grade"></td>
                <td><input type="number" id="weight1" placeholder="Assignment Weight"></td>
            </tr>
        </table>
        <button type="button" onclick="addInputRow()">Add Row</button>
        <button type="button" onclick="calculateAverageGrade()">Calculate</button>
    </form>

    <div id="resultBox">
        <p id="result"></p>
    </div>

    <script>
        let rowCount = 1;

        function addInputRow() {
            rowCount++;
            const table = document.querySelector('table');
            const newRow = table.insertRow(-1);

            const gradeCell = newRow.insertCell(0);
            gradeCell.innerHTML = `<input type="number" id="grade${rowCount}" step="0.01" placeholder="Assignment Grade">`;

            const weightCell = newRow.insertCell(1);
            weightCell.innerHTML = `<input type="number" id="weight${rowCount}" placeholder="Assignment Weight">`;
        }

        function calculateAverageGrade() {
            // Initialize variables to store the sum of (Grade * Weight) and the sum of weights.
            let sumGradeWeight = 0;
            let sumWeights = 0;

            // Loop through each grade and weight input and calculate the sums.
            for (let i = 1; i <= rowCount; i++) {
                const grade = parseFloat(document.getElementById(`grade${i}`).value);
                const weight = parseFloat(document.getElementById(`weight${i}`).value);

                // Check if the grade and weight are valid numbers.
                if (!isNaN(grade) && !isNaN(weight)) {
                    sumGradeWeight += grade * weight;
                    sumWeights += weight;
                }
            }

            // Calculate the average grade.
            if (sumWeights !== 0) {
                const averageGrade = sumGradeWeight / sumWeights;

                // Display the result.
                document.getElementById("result").innerHTML = `Average Grade: ${averageGrade.toFixed(2)}`;
            } else {
                // Handle the case where the sum of weights is zero.
                document.getElementById("result").innerHTML = "Please enter valid grade and weight values.";
            }
        }
    </script>
</body>
</html>

我希望有人立即解决我的疑问。

javascript runtime-error wordpress-theming custom-wordpress-pages wordpress-shortcode
1个回答
0
投票

您是否尝试将 asyncdefer 添加到您的脚本应答器中:

<script async defer>your javascript code here</script>

© www.soinside.com 2019 - 2024. All rights reserved.