当前位置: 代码迷 >> JavaScript >> ,关于jQuery中.ready()和.trigger()有关问题
  详细解决方案

,关于jQuery中.ready()和.trigger()有关问题

热度:516   发布时间:2013-09-28 10:01:20.0
求助,关于jQuery中.ready()和.trigger()问题
是这样的,今天我在看jquery基础教程时,发现了一个奇怪的问题,我写两个了.ready(),在使用.trigger()方法的时候,我把其放在第一个的时候它没效果,然后放在第二个的时候却有效果了,这是为什么呢?
这是jquery代码:
var $body = $('body');
$(document).ready(function(){
var toggleStyleSwitcher = function(event){
if(!$(event.target).is('.button'))
{
$("#switcher .button").toggleClass('hideen');
}
};
$('#switcher').bind("click.collapse",toggleStyleSwitcher);
$('#switcher-narrow,#switcher-large').click(function(){
$('#switcher').unbind('click',toggleStyleSwitcher);
});
$('#switcher-default').click(function(){
$('#switcher').bind('click.collapse',toggleStyleSwitcher);
});

});
$(document).ready(function(){
$('#switcher').trigger('click');
$('#switcher .button').hover(function(){
$(this).addClass('hover').css('background-color','#afa');
},function(){
$(this).removeClass('hover').css('background-color','#fff');
});
$('#switcher').click(function(event){

if($(event.target).is('.button'))
{
$body.removeClass();
if(event.target.id == 'switcher-larage')
{
$body.addClass('large');
}
if(event.target.id == 'switcher-narrow')
{
$body.addClass('narrow');
}
$('#switcher .button').removeClass('selected');
$(event.target).addClass('selected');

}

});
});
这是html代码:
<body>
<div id="switcher">
<h3>Style Switcher</h3>
<div class="button selected" id="switcher-default">
Default
</div>
<div class="button" id="switcher-narrow">
Narrow Column
</div>
<div class="button" id="switcher-large">
Large Print
</div>
</div>


<div id="header">
        <h2>A Christmas Carol</h2>
        <h2 class="subtitle">In Prose, Being a Ghost Story of Christmas</h2>
        <div class="author">by Charles Dickens</div>
      </div>
      
      <div class="chapter" id="chapter-preface">
        <h3 class="chapter-title">Preface</h3>
        <p>I HAVE endeavoured in this Ghostly little book, to raise the Ghost of an Idea, which shall not put my readers out of humour with themselves, with each other, with the season, or with me.  May it haunt their houses pleasantly, and no one wish to lay it.</p>
        <p>Their faithful Friend and Servant,</p>
        <p>C. D.</p>
        <p>December, 1843.</p>
      </div>
              <div class="chapter" id="chapter-1">
        <h3 class="chapter-title">Stave I: Marley's Ghost</h3>
        <p>MARLEY was dead: to begin with. There is no doubt whatever about that. The register of his burial was signed by the clergyman, the clerk, the undertaker, and the chief mourner. Scrooge signed it: and Scrooge's name was good upon 'Change, for anything he chose to put his hand to. Old Marley was as dead as a door-nail.</p>
        <p>Mind! I don't mean to say that I know, of my own knowledge, what there is particularly dead about a door-nail. I might have been inclined, myself, to regard a coffin-nail as the deadest piece of ironmongery in the trade. But the wisdom of our ancestors is in the simile; and my unhallowed hands shall not disturb it, or the Country's done for. You will therefore permit me to repeat, emphatically, that Marley was as dead as a door-nail.</p>
        <p>Scrooge knew he was dead? Of course he did. How could it be otherwise? Scrooge and he were partners for I don't know how many years. Scrooge was his sole executor, his sole administrator, his sole assign, his sole residuary legatee, his sole friend, and sole mourner. And even Scrooge was not so dreadfully cut up by the sad event, but that he was an excellent man of business on the very day of the funeral, and solemnised it with an undoubted bargain.</p>
  相关解决方案