【问题标题】:Including embedded javascript functions from external file包括来自外部文件的嵌入式 javascript 函数
【发布时间】:2019-08-09 17:38:42
【问题描述】:

我正在使用嵌入式 javascript 并在其中定义了某些函数来构建表格:

<% function createCollectionTableRow(col) { %>
<tr>
    <td>
        <a href="/<%= database %>/collections/<%= collections[col][0].name %>"><%= collections[col][0].name %></a></td><td><%= collections[col][0].count %>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_rename_button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#renameModal">Rename</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_rename_button').on('click', function() {
                $('#rename_button').on('click', function() {
                    var newname = $('#rename_input').val();
                    $('#rename').attr('action', '/collections/<%= collections[col][0].name %>/rename?newname=' + newname);
                });
            });
        </script>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_copy_button" class="btn btn-sm btn-info" data-toggle="modal" data-target="#copyModal">Copy</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_copy_button').on('click', function() {
                $('#copy_button').on('click', function() {
                    var selection = $('#select_collection').val();
                    var db = $('#select_database').val();
                    $('#copy').attr('action', '/collections/<%= collections[col][0].name %>/copy?db=' + db + '&copyto=' + selection);
                });
            })
        </script>
    </td>
    <td>
        <form action="/collections/<%= collections[col][0].name %>?_method=DELETE" method="POST">
            <button class="btn btn-sm btn-danger">Delete</button>
        </form>
    </td>
    <td>
        <button id="<%= collections[col][0].name %>_truncate_button" class="btn btn-sm btn-warning" data-toggle="modal" data-target="#docTruncateModal">Truncate</button>
        <script type="text/javascript">
            $('#<%= collections[col][0].name %>_truncate_button').on('click', function() {
                $('#doc_truncate').attr('action', '/collections/<%= collections[col][0].name %>/truncate');
                $('#full_truncate').attr('action', '/collections/<%= collections[col][0].name %>/full_truncate');
            });
        </script>
    </td>
</tr>

现在我可能想在不同的页面上重复使用所说的函数,但是每当我尝试将它移动到一个单独的文件时,比如“functions.ejs”,并在每个页面的开头使用以下语句&lt;% include functions.ejs %&gt;我被告知没有这样的功能。什么给了?

【问题讨论】:

    标签: javascript html ejs embedded-javascript


    【解决方案1】:

    根据README,您必须像这样包含它:

    <% include functions %>
    

    .ejs 不是必需的

    【讨论】:

    • 没有必要但也不相关,它没有什么不同。我仍然收到 create 'createCollectionTableRow is not defined' 错误。
    • 你导出你的函数到functions.ejs了吗? module.exports={createCollectionTableRow}
    • 这是否适用于 ejs?我找到的有关该主题的所有文献都只使用直接的 javascript,我不知道如何导出包含所有 html 和那些
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多