id search works only for first element

后端 未结 2 1404
南旧
南旧 2021-01-17 06:41

I have a block:

2条回答
  •  鱼传尺愫
    2021-01-17 07:20

    I'm pretty sure everything's working correctly with your jQuery, but animating just the opacity doesn't magically change the display style from none to block or whatever the element's value is. I'm pretty sure you set a style for .content to have display: none;. When you animate the opacity, for the divs, their display stays as none, except the first one, because by default, it's overridden with block. Is there any reason you're animating opacity and not using something like fadeIn() and fadeOut?

    Also, since you're storing an id in the href, there's no need to do something like $(id, local_responses)...just use $(id). Take a look at this:

    http://jsfiddle.net/SgwyM/

    var local_links = $(".local_links");
    var local_responses = $(".local_responses");
    
    $(local_links).on("click", "a", function(e){
        e.preventDefault();
        var id = $(this).attr("href");
        local_links.find("div.arrow-selected").remove();
        $(this).parent().append('
    '); $(".content", local_responses).fadeOut(400); $(id).delay(400).fadeIn(400); });

    And just to note, I changed the on binding because this way, it doesn't create an event handler for every found - it creates one for each item in local_links, but is only executed for the selector "a".

提交回复
热议问题