开发者

Need help creating dynamic Javascript Arrays

开发者 https://www.devze.com 2023-01-04 20:54 出处:网络
Part One: I\'m trying to figure out how to use the DOM and Javascript to create an array containing the links in multiple lists.

Part One: I'm trying to figure out how to use the DOM and Javascript to create an array containing the links in multiple lists. The problem is I need each UL to have a unique 开发者_如何学编程array containing the links; the only unique ID I am able to use in this case is the name value in the link tags. I will not be able to add anything else to the markup. The javascript reference will need to be contained in a single script, with one reference to the script at the bottom of the page.

Part Two: What I ultimately need to do, is to hide each of the lists, and replace them with just the first two links, along with a "view all" link below the two links that, when clicked, adds the other two links to the list.

Again, I can't add any markup, divs, etc. etc.; it must be completely based on the information below, the DOM and javascript.

Thanks for any help y'all can provide!

<ul>
    <li>
        <a href="#" name="obj_a">Section One, Article One</a>
    </li>
    <li>
        <a href="#" name="obj_b">Section One, Article Two</a>
    </li>
    <li>
        <a href="#" name="obj_c">Section One, Article Three</a>
    </li>
    <li>
        <a href="#" name="obj_d">Section One, Article Four</a>
    </li>
</ul>

<ul>
    <li>
        <a href="#" name="obj_e">Section Two, Article One</a>
    </li>
    <li>
        <a href="#" name="obj_f">Section Two, Article Two</a>
    </li>
    <li>
        <a href="#" name="obj_g">Section Two, Article Three</a>
    </li>
    <li>
        <a href="#" name="obj_h">Section Two, Article Four</a>
    </li>
</ul>


I am using jQuery for my solutions ;)

Part One:

var list = new Array();
$.each($('ul'), function(index, value) {
  list.push(new Array());
  $.each($(value).find('li a'), function(index2, value2){
    list[list.length - 1].push(value2.href);
  });
});

Part Two:

I really don't understand the requirements, but have a look at jQuery it really makes those takes easy.

0

精彩评论

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

关注公众号