如何通过单击链接(使用 JavaScript)使 div 标签向上或向下移动?

发布于 2024-12-01 18:08:03 字数 850 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2024-12-08 18:08:03

这听起来像是制表符和手风琴模式的组合。看一下这里:

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

十级心震 2024-12-08 18:08:03

我只是想确保我走在正确的轨道上。我怀疑我可以用普通的 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/

如果没有你 2024-12-08 18:08:03

像这样的东西对你有用,概念证明使用document.getElementsByClassnNme

var elements = document.getElementsByClassName('container');

for (var i = 0; i < elements.length; i++) {
    elements[i].onclick = function() {
        var bodies = document.getElementsByClassName('body');
        for (var j = 0; j < bodies.length; j++) {
            bodies[j].style.display = 'none';
        }
        this.children[1].style.display = "block";
    }
}

注意:IE8 或以下版本不支持 getElementsByClassName

jQuery

jQuery('#list1a').accordion();

Something like this would work for you, proof of concept using document.getElementsByClassnNme

var elements = document.getElementsByClassName('container');

for (var i = 0; i < elements.length; i++) {
    elements[i].onclick = function() {
        var bodies = document.getElementsByClassName('body');
        for (var j = 0; j < bodies.length; j++) {
            bodies[j].style.display = 'none';
        }
        this.children[1].style.display = "block";
    }
}

NOTE: no IE8 or below support for getElementsByClassName

Or with jQuery:

jQuery('#list1a').accordion();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文