English 中文(简体)
ivs利用联合材料包装<li>
原标题:Using JS to wrap pairs of <li> in divs

似乎像我需要一些基本的联席援助。 我试图打上一个字母,在<代码><div>上每两个表列加一个<ul>。 清单内容不属我控制,也不属每个要素的高度。 但是,我需要一种办法,确保每个奶牛(边边sh)都接受同样的空间unt。

希望能帮助

谢谢。

因此。

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
</ul>

......

 <ul>
     <div>
        <li></li>
        <li></li>
     </div>
     <div>
        <li></li>
        <li></li>
     </div>
     <div>
        <li></li>
        <li></li>
     </div>
 </ul>
最佳回答

using jquery you can try:

            $(document).ready(function(){               
                var lis = $("li");
                for(var i = 0; i < lis.length; i+=2) {
                    lis.slice(i, i+2).wrapAll("<div></div>");
                }
            });
问题回答

Instead of wrapping in extra non-semantic elements, try using the CSS :even and :odd classes instead. More info found at: Using CSS :even and :odd pseudo-classes with list items

这使预期产出:

<html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    jQuery(document).ready(function(){ 
        var str="",fHtml="";
        var i=0;
        $("li").each(function(key,val){
            str+="<li>"+$(this).html()+"</li>";
            if(++i==2) { 
                    i=0;    
                    fHtml+="<div>"+str+"</div>";
                    str=""; 
                }
         });
         $("ul").html(fHtml)
    });
    </script>
    </head>
    <body>
    <ul>
        <li>first Div</li>
        <li>first Div</li>
        <li>second div</li>
        <li>second div</li>
        <li>third div</li>
        <li>third div</li>
    </ul>
    </body>
    </html>

如果你只是想表明,除其大小外,在母亲身边两处的奶粉中,你只能使用CSS3:

Define float:left for all li elements, and subsequently use the :nth-child(odd) selectedor to Addclear:左; for all odd li elements (1st, 3rd, 5th, 等)

http://jsfiddle.net/e63Rv/“rel=“nofollow” http://jsfiddle.net/e63Rv/。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.