更改动态列表项的CSS

时间:2012-04-06 17:11:29

标签: javascript jquery css css-animations

=== UPDATE ===

如果我从模板中删除the style="display: none;并按照下面的建议应用以下方法,则单击任何其他列表项时会触发空容器。还有什么可以做的?

我有一个使用jQuery和JSON在运行时动态创建的ul列表(使用内联HTML是一个模板)。当用户点击列表项(#navItem)时,我需要更改后台CSS样式。我已经尝试了从内联类到.appentTo()等我可以想到的月亮下的所有内容。下面的内容对于硬编码元素工作正常,但似乎没有任何东西适用于动态加载的内容。更令人困惑的是,li标签中元素中的类启动了...... ???

任何帮助将不胜感激。以下是我的代码片段。日Thnx。

HTML:

<div id="navScrollContainer" class="navContentPosition">
    <ul id="navContent">
    // Display as 'None' to prevent a empty containter from showing -->
        <li id="navItem" class="ulFx" style="display: none;">//<-THIS NEEDS TO CHANGE ONCLICK!!
            <a class="navA">
            <h1 class="navH1">.</h1>
            <h2 class="navH2">.</h2>
            <p class="navP">.</p>
            <hr class="navHR" />
        </li>
    </ul>
</div>
<script type="text/javascript">
    $('#navScrollContainer').on('click', '.ulFx', function() {
        $(this).addClass("liFx");
    });
</script>

这是将数据作为列表注入DOM的函数:

function loadNav(url, container, appendE) {
    $.getJSON(url, function(data) {

        $.each(data.items, function() {
            var newItem = $('#' + container).clone();
            // Now fill in the fields with the data
                    newItem.addClass('ulFx');
            newItem.find("h1").text(this.label);
            newItem.find("h2").text(this.title);
            newItem.find("p").text(this.description);
            newItem.find("a").attr("href", this.gotoURL);
            newItem.children().appendTo('#' + appendE);
        });

        $('#navHeaderTitle').text(data.listTitle);
        iniScroll('scrollNav', 'navScrollContainer');
        var target = data.targetKey;
        // transition("#" + pageName, "show");
    });
};

用户点击项目时需要发生的CSS(仅在该项目上):

@-webkit-keyframes
liBG {from {
    background-color: transparent
}
50% { background-color: rgba(51,102,255,0.15); }
to {
    background-color: transparent
}
}

.liFx {
    -webkit-animation-name: liBG;
    -webkit-animation-duration: 1s;
    -webkit-animation-fill-mode: forwards;
}

给予li项目的类别属性:

.navH1 {
    font-size: 18px;
    color: #FFA500;
    text-decoration: underline;
    font-weight: bold;
    margin-top: 8px;
    margin-bottom: 8px;
    margin-left: 15px;
}
.navH2 {
    font-size: 16px;
    color: #999999;
    font-weight: bold;
    margin-top: 5px;
    margin-bottom: 5px;
    margin-left: 25px;
    font-weight: bold;
}
.navP {
    color: #888;
    font-size: 14px;
    text-align: justify;
    margin-top: 5px;
    margin-bottom: 16px;
    margin-left: 25px;
    margin-right: 10px;
}
.navA {
    text-decoration: none;
}
.navHR {
    border: none;
    background-color: #336;
    height: 1px;
}

4 个答案:

答案 0 :(得分:3)

这将关注动态元素:

$(".liFx").live("click", function() {
  $(this).addClass("liBG");
});

答案 1 :(得分:3)

没有看到你的点击处理程序,我只能推测。但是,通常当问题与动态内容相关并让它们响应刺激时,问题在于如何附加处理程序。

如果您使用.click().trigger('click'),处理程序将直接应用于您调用这些函数的元素。这意味着如果元素当前不存在,则它们将不会收到处理程序。

解决此问题的方法是将事件侦听器附加到始终存在的父元素,然后监视从动态子元素向上传播的事件。您可以通过查看event.target手动执行此操作,但jQuery会像往常一样让我们这么做。

现代jQuery的做法是使用.on()documentation):

$('#someparent').on('click', '#childselector', function() {
   // my handler code
});

jQuery然后在#someparent上附加一个处理程序,当它看到一个针对#childselector的点击时,它就会触发。

如果您要将某个班级应用于#navContent的孩子,且#navContent将始终存在,请执行以下操作:

$('#navContent').on('click', 'li', function() {
     $(this).addClass("liFx");
});

如果#navContent也是动态的,只需在DOM树中更高。

作为旁注,我注意到li的ID为navItem。这听起来很像一个类,而不是一个ID。如果您将拥有多个navItem,则他们不能拥有相同的ID。这是类的用途:

<li class="navItem liFx" style="display: none;">

答案 2 :(得分:0)

我不确定问题出在哪里,但你正在尝试这样做:

$("#navlink").on('click', function() {
     $("#yourselector").css("backgroundColor", "#ddd"); //change the color
});

答案 3 :(得分:0)

我在我的函数中添加了另一个div和addClass()方法以及Jeff B's answer above。如果类被硬编码到标签中,它就不起作用。

<ul id="navContent">
    <li id="navItem" style="display: none;">
        <div>//ADDED THIS TAG TO EXCEPT THE CLASS
            <a>
                <h1>.</h1>
                <h2>.</h2>
                <p>.</p>
                <hr/>
            </a>
        </div>
    </li>
</ul>

在我的js文件中:

$.each(data.items, function() {
    var newItem = $('#' + container).clone();
    // Now fill in the fields with the data
    newItem.find("div").addClass("ulFx");//ADDED THIS METHOD
    newItem.find("h1").text(this.label);
    newItem.find("h2").text(this.title);
    newItem.find("p").text(this.description);
    newItem.find("a").attr("href", this.gotoURL);
    newItem.children().appendTo('#' + appendE);
});