开发者

Assigning a CSSClass to each li element in an unordered list programmatically

I've an unorderedlist something like this.

<ul class="simpleTree">    
  <li>    
  <span>root</span>
  <ul>
  <li >
  <span>Tree Node 1</span>
  <ul>
  <li>
  <span>Tree Node 1-1</span>
  <ul>
  <li>
  <span>Tree Node Ajax 1</span>
  </li>
  <li>
  <span>Tree Node Ajax 2</span>
  </li>
  </ul>
  </li>
  <li>
  <span>Tree Node 1-1</span>
  <ul>
  <li>
  <span>Tree Node Ajax 1</span>
  </li>
  <li>
  <span>Tree Node Ajax 2</span>
  </li>
  </ul>
   </li>
   </ul>
   </li>
   </ul>
   </li>
</ul>

I want to transform it into

<ul class="simpleTree">
  <li class="root">
  <span>
  <a href="#"title="root">root</a</span>                                   <ul>                  开发者_如何转开发                           <li class="open">
<span><a href="test.html" title="this is the title">Tree Node 1</a></span>
<ul>
<li><span>Tree Node 1-1</span>                                      <ul>                                            <li>
<span class="text"><a href="test.html" title="this is the title">Tree Node Ajax 1</a></span>
</li>                                           <li>
<span class="text"><a href="test2.html" title="this is the title for the second page">Tree Node Ajax 2</a></span>
</li>                                           </ul>
                                            </li>
                                            <li><span>Tree Node 1-1</span>
                                            <ul>
                                            <li><span class="text"><a href="test.html" title="this is the title">Tree Node Ajax 1</a></span>
</li>                                           <li>
<span class="text"><a href="test2.html" title="this is the title for the second page">Tree Node Ajax 2</a></span>
</li>                                           </ul>                                           </li>                                           </ul>
</li>
</ul>       
</li>
</ul>

the above format Programmatically using asp.net/C# or jquery by looping through each 'li' element and assigning a css class,anchor tags etc.The above unordered list is just a sample one.Basically I am retrieving around 600 records from the database and transforming them into unordered list.Could someone please suggest how do I do that?

UPDATE:

I am using the following code in asp.net codebehind to generate an unorderedlist from the database records.

int lastDepth = -1;
        int numUL = 0;

        StringBuilder output = new StringBuilder();


        foreach (DataRow row in ds.Tables[0].Rows)
        {

            int currentDepth = Convert.ToInt32(row["Depth"]);

            if (lastDepth < currentDepth)
            {
                if (currentDepth == 0)                    
                    output.Append("<ul class=\"simpleTree\">");                       

                else
                    output.Append("<ul>");
                numUL++;
            }
            else if (lastDepth > currentDepth)
            {
                output.Append("</li></ul></li>");
                numUL--;
            }
            else if (lastDepth > -1)
            {
                output.Append("</li>");
            }

            output.AppendFormat("<li><span>{0}</span>", row["name"]);

            lastDepth = currentDepth;
        }

        for (int i = 1; i <= numUL; i++)
        {
            output.Append("</li></ul>");
        }
literal1.text=output.ToString();

In my database table I've name,depth feild.Using the 'depth' feild I am binding data to the unordered list

Thanks in advance.


JavaScript:

You could use the DOMObject.innerHTML read/write property. Or, jQuery's .append().
For the attributes, the DOMObject.setAttribute() should get you all the way.

Check out this piece of jQuery documentation and this.

Am I missing some functionality you wanted?


you are doing the wrong thing.

instead of that approach, i would suggest a different one

  1. Generate the ul element with an id or class
  2. use that id or class in your style sheet

for e.g

<ul>    
  <li>    
  <span>root</span>
  <li>
</ul>

i would generate like

<ul class='mylist'>    
  <li>    
  <span>root</span>
  <li>
</ul>

my css would contain

ul.mylist {
 write your style properties 
}

ul.mylist li{
 write your style property for li element
}

Then your records will be displayed properly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜