简体   繁体   中英

Javascript array trouble

Its been a long day and I've learnt much about js but my heads fuzzled on this one, ok so I need to get one of the array numbers (depending on the one the user clicks into the settings below. but i have no idea how, ive tried reading up on arrays but i cant get my head around it. :/

Sorry, I need to call a function every time I click on a different link and i need the functions to repond to each div seperatly.

var navLink = new Array();

    navLink[0] ='#one' < this is the Id of an href link.
    navLink[1] ='#two'
    navLink[2] ='#three'
    navLink[3] ='#four'
    navLink[4] ='#five'
    navLink[5] ='#six'  

var navDiv = new Array();

    navDiv[0] ='#content-one' < this is the Id of a content div.
    navDiv[1] ='#content-two'
    navDiv[2] ='#content-three'
    navDiv[3] ='#content-four'
    navDiv[4] ='#content-five'
    navDiv[5] ='#content-six'   

var settings = {

    objSlideTrigger: navLink[x], // link button id
    objSlidePanel: navDiv[x] // slide div class or id
}

I hope i have explained myself well enough... sorry if i havent.

I know this works but it only works for one...

var settings = {

    objSlideTrigger: '#one', // link button id
    objSlidePanel: '#content-one' // slide div class or id
}

Heres a js fiddle that shows the core element of what im trying to achieve. Howeve this is just a single link and content box, i want multiple links and content boxes. that all respond individually to being selected. jsfiddle.net/BeU3U/6/

I think this could work for you. Just make sure that navLinks is always the same length as navDivs .

var navLinks = [
  '#one',
  '#two',
  '#three',
  '#four',
  '#five',
  '#six',
];

var navDivs = [
  '#content-one',
  '#content-two',
  '#content-three',
  '#content-four',
  '#content-five',
  '#content-six',
];

var settings = [];

for (var i=0; i<navLinks.length; i++) {
  settings.push({
    objSlideTrigger: navLinks[i],
    objSlidePanel: navDivs[i],
  });
};

console.log(settings);

Output

[
  {objSlideTrigger: "#one", objSlidePanel: "#content-one"},
  {objSlideTrigger: "#two", objSlidePanel: "#content-two"},
  {objSlideTrigger: "#three", objSlidePanel: "#content-three"},
  {objSlideTrigger: "#four", objSlidePanel: "#content-four"},
  {objSlideTrigger: "#five", objSlidePanel: "#content-five"},
  {objSlideTrigger: "#six", objSlidePanel: "#content-six"},
]

You can probably achieve this with an array of JSON elements.

var nav = [
    {"navLink": "#one", "navDiv": "#content-one"},
    {"navLink": "#two", "navDiv": "#content-two"},
    {"navLink": "#three", "navDiv": "#content-three"},
    {"navLink": "#four", "navDiv": "#content-four"},
    {"navLink": "#five", "navDiv": "#content-five"},
    {"navLink": "#six", "navDiv": "#content-six"}
];

var settings = function (rowIndex) {
    objSlideTrigger: nav[rowIndex]["navLink"],
    objSlidePanel: nav[rowIndex]["navDiv"]
};

try this one below

var settings = function (rowIndex)
{
    objSlideTrigger:navLink[rowIndex], 
    objSlidePanel:navDiv[rowIndex] 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM