使用jQuery有条件地附加到父级

时间:2014-10-11 13:57:48

标签: jquery

我有以下jQuery:

function updateList (day,bt) {
    $.getJSON('index.php?task='+((bt==1)?'getSubmit':'getReceive'),{day:day}, function(data) {
        var $day=$("#todays").empty();
        if(data.length>0) {
            $.each(data, function( i, v ) {
                console.log(v)
                $('<a/>', {
                    href    :'index.php?id='+v['id'],
                    'class' :'linkPreview',
                    title   :'Get Results',
                    text    :v['name']
                })
                .append(bt==1?'':$('<span>'+formatExtra(v.title,v.number)+'</span>'))
                .wrap('<li'+((v['user']==1)?' class="users"':'')+'>').parent().appendTo('#todays');
            });
            $day.find('li:even').addClass('alt');
            $day.find('a.linkPreview').ajaxTip(ayb.linkPreviewObj);
        }
        else {
            $day.html('<li class="no-none">You have no data today</li>');
        }
    });
}

我正在尝试创建:

<ul id="todays" class="list">
    <li class="users alt">
        <a title="Get Results" class="linkPreview" href="index.php?id=5">
            <span>Main Stuff</span>
        </a>
        <span>Extra Stuff</span>
    </li>
    <!-- more list elements .... -->
</ul>

相反,我正在创建:

<ul id="todays" class="list">
    <li class="users-bids alt">
        <a title="Get Results" class="linkPreview" href="index.php?id=5">
            <span>Main Stuff
                <span>Extra Stuff</span>
            </span>
        </a>
    </li>
    <!-- more list elements .... -->
</ul>

我认为我的问题就在这一行:

.append(bt==1?'':$('<span>'+formatExtra(v.title,v.number)+'</span>'))

我将<span>附加到<span>元素中的<a>元素,而不是<a>。{/ p>

有关不起作用的条件,请参阅http://jsfiddle.net/95ztrmk3/1/;有关可行的条件,请参阅http://jsfiddle.net/95ztrmk3(请注意,我对其进行了简化,但意图相同)。< / p>

如何有条件地将元素附加到父元素?

0 个答案:

没有答案