javascript - 更改动态列表项的 CSS

标签 javascript jquery css css-animations

===UPDATE===

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

我有一个 ul 列表,它是在运行时使用 jQuery 和 JSON 动态创建的(使用内联 HTML 是一个模板)。当用户单击列表项 (#navItem) 时,我需要更改背景 CSS 样式。我已经尝试了从内联类到 .appentTo() 等我能想到的一切。下面的内容适用于硬编码元素,但似乎无法处理动态加载的内容。更让人迷惑的是,li标签内的元素中的类发起...???

如有任何帮助,我们将不胜感激。下面是我的代码片段。谢谢。

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>

这是将数据作为列表注入(inject) 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 项的 Class 属性:

.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;
}

最佳答案

这将监视动态元素:

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

关于javascript - 更改动态列表项的 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10046749/

相关文章:

jQuery .show() 向元素添加样式 ="display:inline-block"

javascript [object Object] 到字符串

javascript - jQuery SO 投票对象和范围

javascript - 用鼠标移动放大的图像

css - 如何调整 vuetify 组件中的分隔线宽度?

javascript - 使用 AJAX 在 HTML 中显示 php 结果

javascript - Jquery css 选择器如果没有数据则应用样式

javascript - 使用纬度、经度值创建地理围栏

javascript - 在 Javascript 中替换为 $$

javascript - 如何在 IE 中禁用页面滚动