简体   繁体   中英

How do I get an array to work with a jQuery function?

What am I doing wrong here? I'm trying to get the code to output the code below. I'm a little knew at this, so I don't understand how to fix it, and I tried googling it up but I couldn't find any solution.

var arrayName = [
  $('nav ul li:first-child a'),
  $('nav ul li:last-child a')
]

newsNav[1].on('click', {
  alert('works');
});

Desired Output:

$('nav ul li:first-child a').on('click', {
  alert('works');
});

All the comments above are your answer. So your code should be looks like this...

var arrayName = [$('nav ul li:first-child a'),
  $('nav ul li:last-child a')];

arrayName[1].on('click', function(){
  alert('works');
});

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