无法让JQuery UI Selectable工作

时间:2012-07-22 05:11:43

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

我在使用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是否以某种方式干扰它,但我不确定如何在不破坏格式的情况下修复它。

1 个答案:

答案 0 :(得分:0)

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

当您附加单元格时 - 您使用$('<div/>') - 这将转换为“从dom中获取'<div/>'类型的元素并将其移动到我的'plateActual'元素中” - 您应该添加$,因为您正在创建一个尚不存在的元素。

这样的事情应该有效(http://jsfiddle.net/k3YJY/):

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