Bootstraptable 使用js初始化不成功

发布于 2022-09-05 07:56:15 字数 1272 浏览 13 评论 0

小白一名,这段代码是从BootStraptable介绍页上直接复制下来的,但是还是无法初始化,Console里没有报错,Network里也没有404错误,请问如何才能使用js初始化表格?不甚感激

<html>
    <head>
        <meta charset="utf-8" />
        <link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-table.css">
    <script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/bootstrap-table.js"></script>
<!-- put your locale files after bootstrap-table.js -->
<script src="js/bootstrap-table-zh-CN.js"></script>
<script>
    $('#table').bootstrapTable({
    columns: [{
        field: 'id',
        title: 'Item ID'
    }, {
        field: 'name',
        title: 'Item Name'
    }, {
        field: 'price',
        title: 'Item Price'
    }],
    data: [{
        id: 1,
        name: 'Item 1',
        price: '$1'
    }, {
        id: 2,
        name: 'Item 2',
        price: '$2'
    }]
});
</script>

        <title></title>
    </head>
    <body>
        <table id="table"></table>
    </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2022-09-12 07:56:15

你的脚本执行的时候dom还没有加载完,取不到id="table"的jQuery对象。
当前案例中最简单的解决办法是将你的脚本放到body标签的最后面。

<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <link rel="stylesheet" href="css/bootstrap-table.css">
    </head>
    <body>
        <table id="table"></table>
        <script src="js/jquery.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/bootstrap-table.js"></script>
        <!-- put your locale files after bootstrap-table.js -->
        <script src="js/bootstrap-table-zh-CN.js"></script>
        <script>
            $('#table').bootstrapTable({
                columns: [{
                    field: 'id',
                    title: 'Item ID'
                },
                {
                    field: 'name',
                    title: 'Item Name'
                },
                {
                    field: 'price',
                    title: 'Item Price'
                }],
                data: [{
                    id: 1,
                    name: 'Item 1',
                    price: '$1'
                },
                {
                    id: 2,
                    name: 'Item 2',
                    price: '$2'
                }]
            });
        </script>
    </body>
</html>

因为默认的js加载和执行是阻塞dom渲染的,因此你应该总是将js的引入和内嵌的js脚本放到body的最后。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文