简体   繁体   English

更改动态列表项的CSS

[英]Change CSS of Dynamic List Item

===UPDATE=== ===更新===

If I remove the style="display: none; from the template and apply the below method as recommended below, the empty container fires when you click on any other list item. What else can be done?如果我从模板中删除the style="display: none;并按照下面的建议应用下面的方法,当您单击任何其他列表项时,空容器将触发。还有什么可以做的?

I have an ul list that is dynamically created at run time using jQuery and JSON (Using inline HTML is a template).我有一个 ul 列表,它是在运行时使用 jQuery 和 JSON 动态创建的(使用内联 HTML 是一个模板)。 I need the background CSS style to change when a user clicks on a list item (#navItem).当用户单击列表项 (#navItem) 时,我需要更改背景 CSS 样式。 I've tried everything under the moon that I can think of from inline class to.appentTo(), etc. What I have below works fine for hard-coded elements but Nothing seems to work with dynamically loaded content.我已经尝试了从内联 class 到 .appentTo() 等我能想到的一切。下面的内容适用于硬编码元素,但似乎无法处理动态加载的内容。 Whats even more confusing is that the classes in the elements within the li tag initiate...???更让人迷惑的是,li标签内的元素中的类发起...???

Any help would be appreciated.任何帮助,将不胜感激。 Below are my code snippets.以下是我的代码片段。 Thnx.谢谢。

HTML: 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>

This is the Function that injects the data into the DOM as a list:这是将数据作为列表注入 DOM 的 Function:

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");
    });
};

The CSS that need to happen (only on that item) when the user clicks on a Item: 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;
}

The Class atributes given to the li items:给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;
}

This will watch for dynamic elements:这将监视动态元素:

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

Without seeing your click handler, I can only speculate.没有看到您的点击处理程序,我只能推测。 However, generally when the problem is related to dynamic content and having them respond to stimulus, the problem lies in how you are attaching the handler.但是,通常当问题与动态内容和让它们响应刺激有关时,问题在于您如何附加处理程序。

If you use .click() , or .trigger('click') , the handler will be applied directly to the elements you are calling these functions on.如果您使用 .click .click().trigger('click') ,处理程序将直接应用于您调用这些函数的元素。 That means that if the elements do not currently exist, they will not receive a handler.这意味着如果元素当前不存在,它们将不会收到处理程序。

The way to get around this, is to attach the event listener to a parent element that will always exist and then watch for the event propagating up from the dynamic child element.解决这个问题的方法是将事件侦听器附加到将始终存在的父元素,然后监视从动态子元素向上传播的事件。 You could do this manually, by looking at the event.target , but jQuery, as usual, makes this easy for us.您可以通过查看event.target手动执行此操作,但像往常一样,jQuery 使这对我们来说很容易。

The modern jQuery way of doing this is using .on() ( documentation ):现代 jQuery 这样做的方法是使用.on()文档):

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

jQuery then attaches a handler on #someparent , and when it sees a click that was targeted at #childselector , it fires. jQuery 然后在#someparent上附加一个处理程序,当它看到针对#childselector的点击时,它会触发。

If you want to apply a class to a child of #navContent , and #navContent will always exist, do this:如果您想将 class 应用于 #navContent 的子#navContent ,并且#navContent将始终存在,请执行以下操作:

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

If #navContent is dynamic too, simply go higher in the DOM tree.如果#navContent也是动态的,则只需在 DOM 树中高出 go。

As a side note, I notice that the li has an id of navItem .作为旁注,我注意到li的 id 为navItem This sounds an awful lot like a class, rather than an ID.这听起来很像 class,而不是 ID。 If you are going to have more than one navItem , they cannot all have the same ID.如果您要拥有多个navItem ,它们不能都具有相同的 ID。 This is what classes are for:这是类的用途:

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

I am not sure where is the problem, but you are trying to do something as such:我不确定问题出在哪里,但您正在尝试这样做:

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

I added another div and an addClass() method to my function along with Jeff B's answer above .我向我的 function 添加了另一个 div 和一个 addClass() 方法以及上面 Jeff B 的回答 If the class is hard coded into the tag, it doesnt function.如果 class 被硬编码到标签中,它就不是 function。

<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>

In my js file:在我的 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM