如何通过单击链接(使用 JavaScript)使 div 标签向上或向下移动?
我有 4 个“标题”(它们是一个 div 标签,带有一个链接,可打开每个标题的隐藏区域...这是为了整齐地组织移动设备的所有内容,同时仍然有大量内容)重点是我得到在这里...这些标题在折叠时大小相同,但在展开时大小不同。
我需要做的是单击一个链接,标题会向上移动,单击另一个链接,标题会向下移动。所以我有 1,2,3,4...我在 2 上单击“向上”,现在 2 是 1 所在的位置,1 是 2 所在的位置。
目前,我的标题 1 位于顶部,然后是需要经常编辑的内容,然后是其他 3 个标题。我只想添加一个“向上”链接和一个“向下”链接,将整个 div 与其上面/下面的一个交换
编辑:现在,我
function toggle() {
var ele = document.getElementById("toggleText");
if(ele.style.display == "block") {
ele.style.display = "none";
}
else {
ele.style.display = "block";
}
}
为每个标题(所以 4 个单独的 javascript 函数)
和简而言之,这就是我的页面的样子:
--导航链接
--
~~标题 1~~
内容
~~标题2~~
~~标题3~~
~~标题 4~~
当它们折叠时,只有一个中断标签将它们分开。我想向每个标题添加一个链接,这样当我单击它时,它将与其上面/下面的标题交换位置。
这有助于更好地解释吗?
如果可能的话我更愿意坚持使用 Javascript
I have 4 'headers' (they are a div tag with a link that opens up the hidden area of each header... this is to neatly organize everything for a mobile device while still having alot of content) Point is i'm getting at here... these headers are all the same sized collapsed, but NOT when they are expanded.
What I need to be able to do is to click a link and the header moves up, click another link and it would move down. So i have 1,2,3,4... I click 'up' on 2, and now 2 is where 1 was, and 1 is where two was.
Currently, I have header 1 at the top, then I have content that will be edited quite frequently, then I have the other 3 headers. I just want to add an 'up' link and a 'down' link that'll swap the entire div with the one above/below it
EDIT: Right now, I have
function toggle() {
var ele = document.getElementById("toggleText");
if(ele.style.display == "block") {
ele.style.display = "none";
}
else {
ele.style.display = "block";
}
}
for each of my headers (so 4 seperate javascript functions)
and in short this is what my page is like:
--Nav Links--
~~Header 1~~
content
~~Header 2~~
~~Header 3~~
~~Header 4~~
Theres a only a break tag seperating them while they are collapsed. I want to add a link to each header so when I click it, it will swap positions with the one above/below it.
Does this help explain it any better?
And i'd prefer to stick to Javascript if at all possible
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是制表符和手风琴模式的组合。看一下这里:
http://docs.jquery.com/UI/Accordion
This sounds like a combination of a tab and accordion pattern. Take a look here:
http://docs.jquery.com/UI/Accordion
我只是想确保我走在正确的轨道上。我怀疑我可以用普通的 Javascript 来做到这一点,所以我使用了 jQuery。这是我相信你想要的小提琴?
http://jsfiddle.net/jT2gS/3/
I just want to make sure I'm on the right track with this. I doubt I could do this in vanilla Javascript so I used jQuery. Here's a fiddle of what I believe you want?
http://jsfiddle.net/jT2gS/3/
像这样的东西对你有用,概念证明使用document.getElementsByClassnNme
注意:IE8 或以下版本不支持 getElementsByClassName
或jQuery:
Something like this would work for you, proof of concept using document.getElementsByClassnNme
NOTE: no IE8 or below support for getElementsByClassName
Or with jQuery: