【问题标题】:Can't get JQuery UI Selectable to work at all根本无法让 JQuery UI Selectable 工作
【发布时间】:2012-07-22 05:11:42
【问题描述】:

我在选择 JQuery UI 以用于我的网站时遇到了一些麻烦。我对编码还是很陌生,我不确定我做错了什么。我搜索了相关问题,但找不到任何让我弄清楚的问题。

我正在尝试创建一个本质上为工作而美化的标签制造商,并希望能够使用鼠标“套索”选择网格中的单元格。但由于某种原因,我根本无法选择工作。

这里是js:

    $('document').ready(function() {

        for (i=0; i<117;i++)    {
                $('#plateActual').append('<div class="cell"/>');
                }

                $('#plateActual').selectable();
        });

这是 HTML:

    <!DOCTYPE html>
<html>
        <head>
                    <title></title>
                    <link rel="stylesheet" href="style.css" />
                    <link type="text/css" href="css/smoothness/jquery-ui-1.8.21.custom.css" rel="Stylesheet" /> 
                    <script type="text/javascript" src="js/jquery-ui-1.8.21.custom.min.js"></script>
                    <script type="text/javascript"  src="script.js"></script>
                    <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>

        </head>

        <body>
        <div id="wrapper">

                 <div id="navbar">      
                 </div>
                 <div id="controlPanel">
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>
                 <p>test<br>test<br>test<br>test<br>test<br>test<br></p>

                 </div>
                 <div id="plateEditor">
                            <div id="plateActual">

                            </div>

                 </div>
                 <div id="bottom">
                 </div>
        </div>

        <script type="text/javascript"  src="script.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.21.custom.min.js"></script>
        <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>

        </body>

这是 CSS:

 body   {
        margin: 0px auto;
        padding: 0;
}


p       {
        color: yellow;
}

#wrapper    {
                border: 1px dotted black;
                width: 980px;
                margin: 0px auto;
                padding: 0;
}

#navbar  {
             width: 980px;
             background: black;
             height: 50px;
             position: fixed;
             margin: 0;
             padding: 0;
             top: -10px;
}

#controlPanel        {

                                 width: 250px;
                                 background-color: red;
                                 float:left;
                                 top: 100px;    
                                 margin-bottom: 0;

}

#plateEditor             {
                                 position:    relative;
                                 overflow: auto;
                                 width: 730px;
                                 float:right;
                                 top: 100px;
                                 margin-bottom: 0;
                                 margin-top: 150px;
}

#plateActual             {
                                 border: 1px solid;
                                 width: 403px;
                                 height: 279px;
                                 margin: 0px auto;
                                 pading: 0;
}

#bottom                      {
                                 width: 980px;
                                 height: 30px;
                                 background:green;
                                 clear: both;
                                 bottom: 0;
                                 margin: 0px auto;
                                 padding: 0;
}

.cell                            {
                                 float: left;
                                 width: 29px;
                                 height: 29px;
                                 border: 1px    dotted;

}   

如果我的代码杂乱无章,我深表歉意。我仍在研究如何最好地保持其井井有条,并通常以可接受的方式编写它。非常感谢你的帮助。

更新:我已经做出了 Ando 建议的更改,但我仍然无法选择工作。我尝试将 #plateActual 更改为 ol 或 ul 并附加 li.cell ,但这也不起作用。

我想知道原因是否是我的 css 以某种方式干扰了它,但我不确定如何在不破坏格式的情况下解决这个问题。

【问题讨论】:

    标签: javascript jquery jquery-ui jquery-ui-selectable


    【解决方案1】:

    append 返回的元素将是执行添加的元素 - 在您的情况下为 plateActual- 因此您会将 cell 类添加到错误的元素中。

    当您附加单元格时-您使用$('&lt;div/&gt;')-这将转换为“从dom中获取'&lt;div/&gt;'类型的元素并将它们移动到我的'plateActual'元素中”-您应该按原样添加不带$创建一个尚不存在的元素。

    这样的事情应该可以工作(http://jsfiddle.net/k3YJY/):

    for (var i=0; i<5;i++)    {                          
              $('#plateActual').append('<div class="cell"/>');                                           
    }
    

    【讨论】:

    • 非常感谢。这似乎是一种更明智的创建 div 的方式。不幸的是,我仍然无法选择工作,而且我在这方面没有取得任何进展。
    • 究竟是什么不工作?你调试了吗?也许你有错误 - 使用 firebug 或内置的 chrome/opera 调试器查看。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 2011-09-19
    相关资源
    最近更新 更多