Mathematical way of getting set of numbers from one number
I am using javascript, but I think that the idea applies across all languages. The idea is if you have 1 of 4 numbers you can get the other numbers. Something like this:
1 = 234
2 = 134
3 = 124
4 = 123
If you have 1 you output 234, and etc. whats the most efficient way to do this? Do you have to use arrays, loops?
something like:
var fruits = ["#apple", "#orange", "#peach"];
for (i = 0; i <=开发者_如何学C fruits.length-1; i++) {
$(fruits[i]).show;
$(fruits[WANT TO BE not i]).hide;//on loop would want to be other number besides i
$(fruits[WANT TO BE not i]).hide;//on loop would want to be other number besides i
}
I don't know about efficiency, but since you are already using jquery, why not do:
<div id="apple" class="fruit">...</div>
<div id="orange" class="fruit">...</div>
<div id="peach" class="fruit">...</div>
$('.fruit').click(function() {
$('.fruit').hide();
$(this).show();
});
EDIT:
If I correctly identify your need, you want to show slide 2 everytime users click on 2 on Edit Slides.
In that case, you can attach id and class to the slides you want to show, like so:
<div id="slide_1" class="slide"><img src="1.jpg" /></div>
<div id="slide_2" class="slide"><img src="2.jpg" /></div>
<div id="slide_3" class="slide"><img src="3.jpg" /></div>
and add rel to your a
<ul class="navigationList">
<li><a href="#" rel="slide_1">1</a></li>
<li><a href="#" rel="slide_2">2</a></li>
<li><a href="#" rel="slide_3">3</a></li>
</ul>
Now the javascript:
$('ul.navigationList li a').click(function(e) {
e.preventDefault();
var slideId = $(this).attr('rel');
$('.slide').hide();
$('#' + slideId).show();
return false;
});
Again this is only one of the ways of doing it.
I really think it's overkill, but if you need to do this with a use case that I can't imagine, you can always do this (using your fruits example):
var fruits = ["#apple", "#orange", "#peach"];
function showOne(n)
{
for(i = 0; i < fruits.length; ++i)
{
if(n != i)
{
$(fruits[i]).hide();
}
else
{
$(fruits[i]).show();
}
}
}
$(document).ready(function(){
showOne(2);
});
Fiddle here
Edit:
A few things based on viewing your source (1 and 2 are just my own opinion :)):
- There's no question that I'd break all of those into individual pages. Not only do they not logically belong together, but I would hate to see the end result code (horrendous bloat).
- On top of splitting the pages up, I would also have most of that functionality in the back end, not client side.
- If you're set on doing this all client side, then I'd actually completely change the logic that you have:
HTML:
<a onclick="nav('slides'); return false;" href="#slides">Edit Slides</a>
<a onclick="nav('pages'); return false;" href="#pages">Edit Pages</a>
<a onclick="nav('projects'); return false;" href="#projects">Edit Projects</a>
Script:
function nav(panel)
{
$('.section').hide();
$(panel).show();
}
KISS. No reason to over-complicate things :)
精彩评论