如何动态地将div附加到ul

时间:2015-01-31 12:20:52

标签: jquery

我需要关注HTML,如下所示 如下面这个小提琴

http://jsfiddle.net/7j61bxbv/

<h3>Test <a class="icon-pencil-1 labelEditIcon"></a></h3>

<ul data-role="listview" class="labellistUL">        
    <li class="labellist">
        <div class="leftlable">
             <h4>Rayalaseema Ruchulu</h4>

            <p>Area Name, <b>Phone:</b> +91 99890 11223</p>
            <p><b>Timings:</b> 11am - 2pm</p>
            <p class="minOrder">Min. Order Rs. 250/- Delivery Charges Free</p>
            <label class="label-red label-wrap">Close</label>
        </div>
        <div class="rightlable"> <a href="#">Remove</a>

        </div>
    </li>
    <div class="label-BtnWrap">
        <button class="icon-plus btn btn-c">Add More</button>
    </div> 
</ul>

我可以生成以下HTML

http://jsfiddle.net/m5f9c877/

<h3>Test<a class="icon-pencil-1 labelEditIcon"></a></h3>
<ul data-role="listview" class="labellistUL"></ul>
<li class="labellist">
   <div class="leftlable">
      <h4>Rayalaseema Ruchulu</h4>
      <p>Area Name,  <b>Phone:</b> +91 99890 11223</p>
      <p><b>Timings:</b> 11am - 2pm</p>
      <p class="minOrder">Min. Order Rs. 250/-  Delivery Charges Free</p>
      <label class="label-red label-wrap">Close</label>
   </div>
   <div class="rightlable">
      <a href="#">Remove</a>
   </div>
</li>
</ul>

但是我无法在ul标签

之前放置以下div
<div class="label-BtnWrap">
        <button class="icon-plus btn btn-c">Add More</button>
    </div> 

Could you please let me know how to do this ??

2 个答案:

答案 0 :(得分:0)

<强> Manual

var btn = '<div class="label-BtnWrap"><button class="icon-plus btn btn-c">Add More</button></div>';    
$("ul.labellistUL li:last-child").after(btn);

<强> Demo

答案 1 :(得分:0)

这不是jquery ...是带有HTML concat的纯javascript ...我想你想要删除</ul>中的var html = '<h3>Test<a class="icon-pencil-1 labelEditIcon"></a></h3><ul data-role="listview" class="labellistUL"></ul>';,因为你在{</ul>中结束了html += '</ul>' 1}}

但对于jQuery解决方案,最好的选择是:

var ulElement = $('ul.labellistUL');

for(var i=0;i<response.length;i++)
{
var vendor_id = response[i].vendor_id;
if(vendor_id)
{
   var liToAdd = $('<li class="labellist">
        <div class="leftlable">
        <h4>Rayalaseema Ruchulu</h4>
        <p>Area Name,  <b>Phone:</b> +91 99890 11223</p>
        <p><b>Timings:</b> 11am - 2pm</p>
        <p class="minOrder">Min. Order Rs. 250/-  Delivery Charges Free</p>
        <label class="label-red label-wrap">Close</label>\n\
        </div>
        <div class="rightlable">
        <a href="#">Remove</a>
        </div>
        </li>');

   ulElement.append(liToAdd);
}
}