【问题标题】:Loading script from .js file从 .js 文件加载脚本
【发布时间】:2012-12-31 20:25:37
【问题描述】:

我正在尝试从 .js 文件加载 .htm 模板。但是 .htm 文件中存在一个脚本,该脚本在加载模板并且一切顺利时触发。

模板的外观如下。 testing.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>http://stackoverflow.com/questions/6946559/jqgrid-please-help</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <script type="text/javascript" charset="utf-8">
            $(document).ready(function () {
                $('#example').dataTable({
                    "bProcessing": true,
                    "sAjaxSource": '/Home/GetData',
                    "sScrollY": "400px",
                    "sScrollX": "200px",
                    "bPaginate": false
                });
            });
        </script>

 </head>
<div id="dynamic">
<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">
    <thead>
        <tr>
            <th width="20%">Date</th>
            <th width="25%">Name</th>
            <th width="25%">ProposalID</th>
            <th width="25%">Time</th>
               </tr>
    </thead>
    <tbody>
   </tbody>

</table>
</div>
</html>

这是加载模板的 .js 文件。

var iTabs = function () {
    return {
        Init: function () {

            var placeholder = $("#testtab");
            placeholder.setTemplateURL("/Templates/Home/testing.htm");

            placeholder.load("/Templates/Home/testing.htm");


        }
    }
} ();

但是,现在我想在 .js 文件中执行 .htm 脚本,即在加载模板之后。 如果我只运行脚本的一部分,即

$('#example').dataTable({
                        "bProcessing": true,
                        "sAjaxSource": '/Home/GetData',
                        "sScrollY": "400px",
                        "sScrollX": "200px",
                        "bPaginate": false
                    });

在 .js 文件中,它不会工作。是否可以在 .js 文件中运行此脚本?如果有怎么办?

【问题讨论】:

    标签: javascript jquery html asp.net-mvc datatables


    【解决方案1】:

    尝试使用load的回调运行该js代码

    像这样:

    placeholder.load("/Templates/Home/rpt.htm", function() {
      $('#example').dataTable({
                        "bProcessing": true,
                        "sAjaxSource": '/Home/GetData',
                        "sScrollY": "400px",
                        "sScrollX": "200px",
                        "bPaginate": false
                    });
    });
    

    欲了解更多信息,refer to the jQuery load docs

    【讨论】:

    • 它给我带来了错误哎呀!您正在查看的页面不存在。 :(
    • 我们不能像先加载网格然后渲染数据那样单独加载吗?
    • 您正在查看的页面不存在 = 提供的路径错误...尝试使用它,例如 Templates/Home/rpt.htm./Templates/Home/rpt.htm
    • 是的,我会检查它,我想知道一件事。这将在加载文档时触发。如果我需要在单击按钮时显示数据怎么办?万一怎么办?
    猜你喜欢
    • 2018-11-15
    • 2018-12-23
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多