【问题标题】:jQuery swap dom position and append datajQuery 交换 dom 位置并追加数据
【发布时间】:2013-02-01 13:17:41
【问题描述】:

我正在开发一个新网站。 现在我有一个固定位置的菜单列表:

菜单 html

<ul id="work-nav">
    <li class="selected">   
        <span class="label">new</span>
        <a href="#cake-film">Cake Film</a>
    </li>
    <li>
        <a href="#LOS-BANGELES">Los Bangeles</a>
    </li>
    <li>
        <a href="#museumnacht">Museumnacht - N8</a>
    </li>
    <li>
        <a href="#crosby-stills-nash">Crosby, Stills &amp; Nash</a>
    </li>
    <li>
        <a href="#meltinpot">Meltin'Pot</a>
    </li>
    <li>
        <a href="#nstore">N-store</a>
    </li>
    <li>
        <a href="#viewbook">Viewbook</a>
    </li>
    <li>
        <a href="#foodnotes">Foodnotes</a>
    </li>
</ul>

我想要它做的是当文章的顶部(带有链接锚的ID):

文章 html

<article id="#LOS-BANGELES" class="page">
    <div class="wrap">
        <div class="info">
            <p>Los Bangeles Sed eu mauris nibh. Nunc sit amet mauris vitae nibh ultricies
                volutpat id a massa. Nulla lobortis odio vel velit eleifend at elementum.
                Sed eu mauris nibh. Nunc sit amet mauris vitae nibh ultricies volutpat
                id a massa.</p>
            <p>Nulla lobortis odio vel velit eleifend at elementum</p>
        </div>
    </div>
</article>

点击视口顶部,它应该在相关列表项下方附加文章.info div。像这样。

所需的结果 html

<li class="selected">
    <a href="#LOS-BANGELES">Los Bangeles</a>
</li>
<li class="info">THE INFO FROM THE LOS BANGELES ARTICLE</li>

我如何实现这一目标?我正在使用航点来检查锚点是否在视口中。

Jquery 解决方案?

$('#work article').waypoint(function (d) {
    var $active = $(this);
    if (d === "up") {
        $active = $active.prev();
    }
    if (!$active.length) $active = $active.end();

    $('.active').removeClass('active');
    $active.addClass('active');
    /* 
    SOMEWHERE HERE I NEED TO REMOVE THE.info LI FROM THE OLD SELECTED MENU ITEM, IF EXSISTS
    AND APPEND A NEW ONE BASED ON THE ARTICLE.info DIV, BUT HOW
    */
    $('.selected').removeClass('selected');
    $('a[href=#' + $active.attr('id') + ']').addClass('selected');
});

【问题讨论】:

  • 谢谢,还是觉得markdown标签索引有点难

标签: jquery dom jquery-waypoints


【解决方案1】:
// Removes any existing info items from nav
$('#work-nav .info').remove();

// Copies contents of the info div in the currently active article
var $infoContents = $active.find('.info').contents().clone();

// Create a new li.info to inject into the nav;
var $infoLI = $('<li class="info"></li>');

// Set the content of that li to our copy of the article info
$infoLI.append($infoContents);

// Add the new info li after the nav item wrapper our matched link
$('a[href=#' + $active.attr('id') + ']').closest('li').after($infoLI);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2011-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    相关资源
    最近更新 更多