【问题标题】:chosen jquery plugin basic ui does not work选择的 jquery 插件基本 ui 不起作用
【发布时间】:2013-12-28 05:14:05
【问题描述】:

我想使用选择的 jquery 插件,就像他们在官方网站中使用的相同示例:http://harvesthq.github.io/chosen/

这些是我的文件:

这是 index.html:

<html>
    <body>
        <head>
            <script src="jquery-1.9.1.js"></script>
            <script src="chosen.jquery.js"></script>
            <link rel="stylesheet" href="chosen.css">
        </head>
        <body>
            <script type="text/javascript">
                $(".chosen-select").chosen()
            </script>


            <select class="chosen-select" tabindex="8" multiple="" style="width:350px;" data-placeholder="Your Favorite Types of Bear">
                <option value=""></option>
                <option>American Black Bear</option>
                <option>Asiatic Black Bear</option>
                <option>Brown Bear</option>
                <option>Giant Panda</option>
                <option selected="">Sloth Bear</option>
                <option disabled="">Sun Bear</option>
                <option selected="">Polar Bear</option>
                <option disabled="">Spectacled Bear</option>
            </select>
        </body>
    </body>
</html>

结果如下所示:

错了吗?我看到官方页面生成的html代码,和不一样。当我将代码更改为这个时:

<html>
    <head>
        <script src="jquery-1.9.1.js"></script>
        <script src="chosen.jquery.js"></script>
        <link rel="stylesheet" href="chosen.css">

        <script type="text/javascript">
            $(".chosen-select").chosen()
        </script>
    </head>
    <body>
        <select data-placeholder="Your Favorite Types of Bear" style="width:350px; display:none" multiple class="chosen-select" tabindex="-1">
            <option value=""></option>
            <option>American Black Bear</option>
            <option>Asiatic Black Bear</option>
            <option>Brown Bear</option>
            <option>Giant Panda</option>
            <option selected>Sloth Bear</option>
            <option disabled>Sun Bear</option>
            <option selected>Polar Bear</option>
            <option disabled>Spectacled Bear</option>
        </select>

        <div class="chosen-container chosen-container-multi" style="width: 350px;" title="">
            <ul class="chosen-choices">
                <li class="search-field">
                    <input class="default" type="text" style="width: 183px;" autocomplete="off" value="Your Favorite Types of Bear" tabindex="8">
                </li>
            </ul>

            <div class="chosen-drop">
                <ul class="chosen-results">
                    <li class="active-result" data-option-array-index="1" style="">American Black Bear</li>
                    <li class="active-result" data-option-array-index="2" style="">Asiatic Black Bear</li>
                    <li class="active-result" data-option-array-index="3" style="">Brown Bear</li>
                    <li class="active-result" data-option-array-index="4" style="">Giant Panda</li>
                    <li class="active-result" data-option-array-index="5" style="">Sloth Bear</li>
                    <li class="disabled-result" data-option-array-index="6" style="">Sun Bear</li>
                    <li class="active-result" data-option-array-index="7" style="">Polar Bear</li>
                    <li class="disabled-result" data-option-array-index="8" style="">Spectacled Bear</li>
                </ul>
            </div>
        </div>          
    </body>
</html>

我知道了:

我是否需要做其他事情才能获得与官方示例相同的结果?

【问题讨论】:

    标签: javascript jquery html jquery-chosen


    【解决方案1】:

    此代码不起作用的原因是它出现在源顺序中的选择元素之前 - 因此当脚本运行时,DOM(文档对象模型)中没有匹配的元素来应用所选插件方法。

    现在许多开发人员将他们的脚本放在页面底部——就在结束 body 元素标记之前——这既有助于确保您希望与之交互的元素当时在 DOM 中,又可以提高浏览器的性能当它到达脚本时将停止加载 DOM 或任何其他资产(这些通常是并行/同时加载的),仅在它执行时恢复。这主要是出于遗留原因,开发人员会使用 document.write 向页面动态添加内容 - 期望此内容应该出现在脚本所在的位置,而不是在 write 语句执行时浏览器碰巧到达构建 DOM 的位置调用。

    我会将您的第一个示例更改如下-

    <!doctype html>
    <html>
        <body>
            <head>
                <title>Add a title</title>
                <link rel="stylesheet" href="chosen.css">
            </head>
            <body>
                <select class="chosen-select" tabindex="8" multiple style="width:350px;" data-placeholder="Your Favorite Types of Bear">
                    <option value=""></option>
                    <option>American Black Bear</option>
                    <option>Asiatic Black Bear</option>
                    <option>Brown Bear</option>
                    <option>Giant Panda</option>
                    <option selected>Sloth Bear</option>
                    <option disabled>Sun Bear</option>
                    <option>Polar Bear</option>
                    <option disabled>Spectacled Bear</option>
                </select>
    
                <script src="jquery-1.9.1.js"></script>
                <script src="chosen.jquery.js"></script>
                <script>
                    $(document).ready(function() {
                        $(".chosen-select").chosen();
                     });
                 </script>
        </body>
    </html>
    

    这有两种工作方式。首先,脚本放置在页面的末尾,因此选择元素在运行时应该已经在 DOM 中。其次,文档就绪事件处理程序保证我们创建的匿名函数在浏览器完全加载/构建 DOM 之前不会运行。代码在函数中,否则浏览器会立即执行(并且会出现语法错误,因为 ready 方法需要函数作为参数)。文档就绪事件(在现代浏览器中称为 DOMContentLoaded)比 window.onload 更可取,因为它会触发 before 窗口加载事件,可能在浏览器仍在加载页面所需的图像或其他大型资源时.这意味着您的页面不太可能在用户开始与之交互后突然更改。

    其他几点-

    • 您的文档中肯定需要文档类型
    • 尝试始终将样式表放在脚本之前
    • 您有两个结束正文元素标签
    • 任何时候只能选择一个选择选项 - 具有选中的属性。
    • multiple、selected和disabled属性为布尔属性,不需要取值(可以去掉="")

    查看http://learn.jquery.com/using-jquery-core/document-ready/了解更多

    【讨论】:

      【解决方案2】:

      index.html试试这个

      $(window).load(function(){
      $(".chosen-select").chosen()
      });
      

      DEMO

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-14
        • 2023-03-06
        • 2016-03-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多