简体   繁体   English

jQuery操作触发一次,但不会触发一次

[英]jquery actions firing once, but not again

forgive the code being bulkier than necessary, will tidy it up in due course. 原谅多余的代码,将在适当的时候进行整理。

everything seems to be working and yet, when you click a link after it's content has already been 'active' nothing happens. 一切似乎都正常,但是,当您单击链接的内容已经“处于活动状态”后,则什么也没有发生。

i'm sure it's something simple but i can't see it. 我敢肯定这很简单,但我看不到。

EDIT: the following code now works in FF and Chrome, but does not work in IE8. 编辑:以下代码现在可在FF和Chrome中使用,但在IE8中不可用。 Any ideas? 有任何想法吗?

$(function(){
            //initialize active link to not have a link on ready.
            var linkId = $('#pricing_body div.active').attr('id');
            var activeLink = $('#pricing_nav ul li#'+linkId+' a'); //just the link itself.
            activeLink.parent().addClass('activeSection');
            //var activeLinkContents = activeLink.parent().html(); //the link contained in the the list item and it's contents.
            //alert(activeLinkContents);
            var linkContents = activeLink.html(); //text content of the link.
            activeLink.parent().html(linkContents);

            //when link is clicked, store it's text for assignment after <a> is stripped out.
            $('#pricing_nav ul li a').live('click',function(){
                var clickedId = $(this).parent().attr('id');
                var clickedLinkContents = $(this).html();
                $(this).parent().addClass('activeSection');
                $(this).parent().html(clickedLinkContents); //replaces <a><span>name</span></a> with just the span and text.

                //fadeOut active div and give it inactive class. get list item with same id as div we are fading out.
                $('#pricing_body div.active').fadeOut('500',function(){
                    $(this).removeClass('active').addClass('inactive');
                    var divId = $(this).attr('id');
                    var sisterLink = $('#pricing_nav ul li#'+divId);
                    sisterLink.removeClass('activeSection');
                    sisterLink.html('<a href="#">'+sisterLink.html()+'</a>'); //put link in between <li>.

                    //fadeIn the div with id of the link that has been clicked.
                    $('#pricing_body div#'+clickedId).fadeIn('500',function(){
                        $(this).addClass('active').removeClass('inactive');
                        var newActive = $('#pricing_nav ul li#'+clickedId);
                    });
                });
            });
        });

Use live method to attach events to the elements. 使用live方法将事件附加到元素。 Here is the documentation. 是文档。

Try: 尝试:

$('#pricing_nav ul li a').live('click', function(){

---------
---------
---------

});

EDIT: 编辑:

In reply of comment. 回复评论。

.live() 。生活()

The .live() method is able to affect elements that have not yet been added to the DOM through the use of event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. .live()方法能够通过使用事件委托来影响尚未添加到DOM的元素:绑定到祖先元素的处理程序负责在其后代上触发的事件。

.bind() .bind()

The .bind() method is the primary means of attaching behavior to a document. .bind()方法是将行为附加到文档的主要方法。 All JavaScript event types, such as focus, mouseover, and resize, are allowed for eventType. eventType允许使用所有JavaScript事件类型,例如焦点,鼠标悬停和调整大小。

Here is SO Question on this difference. 这是关于这一区别的问题。

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

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