在jquerymobile中动态创建的评级星插件中未正确应用样式

时间:2012-02-06 02:06:39

标签: jquery css jquery-mobile

我正在使用jquery rating star插件,当我静态使用时一切正常,但是当我动态创建listview并将“option”输入控件置于其中时,它从不应用样式。

我已加载样式表:

<head>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
   <meta name="viewport" id="viewport" content="width=device-width,height=device-height,initial-scale=1.0,user-scalable=no"/> 
   <link rel="stylesheet" href="js/rating/jquery.rating.css" />
   <link rel="stylesheet" href="css/themes/etensr.theme.css" />
   <script src="jquery.1.7.1.min.js" type="text/javascript"></script>       
   <script type="text/javascript" src="js/rating/jquery.rating.js"></script> 
   <script type="text/javascript" src="jquery.mobile.1.0.min.js"></script>       
</head>

和页面:

<div data-role="page" id="subcategories" data-theme="a">
    <div class="header" data-role="header" style="height: 42px;" >  
        <a data-rel="back" data-role="button" data-theme="c">Back</a>
        <a href="order.html" data-role="button" data-theme="c" data-icon="gear" data-iconpos="right" class="ui-btn-right">0.00 SRD - </a>                                 
    </div>        
    <div data-role="content" id="subcategoryCnt">
      <ul id="subcategoryList" data-role="listview" data-theme="a" data-dividertheme="c" >
        <li data-icon="arrow-r" > 
           <img src="images/thumb80x80.jpg" />
           <div>
              <div style="float:left;"><p>Product</p></div>
              <div style="float:right;font-size: 12px;">Price</div>    
              <div style="clear:both;"></div>
              <div style="float:left;">
                 <input name="star1" type="radio" data-role="none" class="star" disabled="disabled" />
                 <input name="star1" type="radio" data-role="none" class="star" disabled="disabled" />
                 <input name="star1" type="radio" data-role="none" class="star" disabled="disabled" checked="checked"/>
              </div>
           </div>           
        </li>    
      </ul>                    
    </div>
</div>

所有这些都正确显示但是当我动态加载listview项目时,输入的无线电不会像星星一样呈现,而是正常的无线电按钮:

 <div data-role="page" id="subcategories" data-theme="a">
    <div class="header" data-role="header" style="height: 42px;" >  
        <a data-rel="back" data-role="button" data-theme="c">Back</a>
        <a href="order.html" data-role="button" data-theme="c" data-icon="gear" data-iconpos="right" class="ui-btn-right">0.00 SRD - </a>                                 
    </div>        
    <div data-role="content" id="subcategoryCnt">
        <script type="text/javascript">
           GetSubcategories(); 
        </script>                                    
    </div>
 </div>

和JS是:

function GetSubcategories() {
   subcategories = result.GetSubcategoriesResult;
   if (subcategories.length > 0) {
      var html = '<ul id="subcategoryList" data-role="listview" data-theme="a" data-dividertheme="c" >';
      for (i = 0; i < subcategories.length; i++) {
          html = html + '<li data-role="list-divider">' + subcategories[i].Name + '</li>';
          prods = subcategories[i].Products;
          if (prods.length > 0) {
              for (j = 0; j < prods.length; j++) {
                  html = html +
                      '<li data-icon="arrow-r" >' +
                        '<a href="products.html"  rel="external">' +
                            '<img src="images/thumb80x80.jpg" />' +
                            '<div>' +
                               '<div style="float:left;"><p>' + prods[j].Name + '</p></div>' +
                               '<div style="float:right;font-size: 12px;">' + prods[j].Price + ' SRD</div>' +
                               '<div style="clear:both;"></div>' +
                               '<div style="float:left;">' +
                                    '<input name="star' + prods[j].Id + '" type="radio" data-role="none" class="star" disabled="disabled" />' +
                                    '<input name="star' + prods[j].Id + '" type="radio" data-role="none" class="star" disabled="disabled" />' +
                                    '<input name="star' + prods[j].Id + '" type="radio" data-role="none" class="star" disabled="disabled" checked="checked"/>' +
                               '</div>' +
                            '</div>' +
                        '</a>' +
                      '</li>';
              }
          }
      }
          html += '</ul>';
   }
   $('#subcategoryCnt').html(html);
   $('#subcategoryList').listview();    
}

当我做同样的事情但是动态一切都正常显示,输入无线电除了像星星一样显示。

我做错了什么? 感谢

2 个答案:

答案 0 :(得分:1)

data-icon="star"

怎么样?

另外我认为你还需要刷新jQM

JS

$('#mylist').listview('refresh');

答案 1 :(得分:0)

尝试使用实时查询,因为当你执行$(selector).rating(); ,插件应用于DOM中存在的选择器定义的元素。当您动态添加它们时,您还需要动态地将插件应用于它们。这就是livequery可以帮助你的地方。做点什么

  

$(选择器).livequery(函数(){
                     $(本).rating(); //或者添加你想要的选项            });