是这样的,今天我在看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>
        <p>The mention of Marley's funeral brings me back to the point I started from. There is no doubt that Marley was dead. This must be distinctly understood, or nothing wonderful can come of the story I am going to relate. If we were not perfectly convinced that Hamlet's Father died before the play began, there would be nothing more reable in his taking a stroll at night, in an easterly wind, upon his own ramparts, than there would be in any other middle-aged gentleman rashly turning out after dark in a breezy spot&mdash;say Saint Paul's Churchyard for instance&mdash; literally to astonish his son's weak mind.</p>
        <p>Scrooge never painted out Old Marley's name. There it stood, years afterwards, above the warehouse door: Scrooge and Marley. The firm was known as Scrooge and Marley. Sometimes people new to the business called Scrooge Scrooge, and sometimes Marley, but he answered to both names. It was all the same to him.</p>
        <p>Oh! But he was a tight-fisted hand at the grind-stone, Scrooge! a squeezing, wrenching, grasping, scraping, clutching, covetous, old sinner! Hard and sharp as flint, from which no steel had ever struck out generous fire; secret, and self-contained, and solitary as an oyster. The cold within him froze his old features, nipped his pointed nose, shrivelled his cheek, stiffened his gait; made his eyes red, his thin lips blue; and spoke out shrewdly in his grating voice. A frosty rime was on his head, and on his eyebrows, and his wiry chin. He carried his own low temperature always about with him; he iced his office in the dog-days; and didn't thaw it one degree at Christmas.</p>
        <p>External heat and cold had little influence on Scrooge. No warmth could warm, no wintry weather chill him. No wind that blew was bitterer than he, no falling snow was more intent upon its purpose, no pelting rain less open to entreaty. Foul weather didn't know where to have him. The heaviest rain, and snow, and hail, and sleet, could boast of the advantage over him in only one respect. They often "came down" handsomely, and Scrooge never did.</p>
         </div>
</body>
这是css代码:
body.large .chapter{
font-size:1.5em;
}
body.narrow .chapter{
width:400px;
}
.chapter {
  margin: 1em;
}#switcher {
  float: right;
  background-color: #ddd;
  border: 1px solid #000;
  margin: 10px;
  padding: 10px;
  font-size: 0.9em;
}
#switcher h3 {
  margin: 0;
}
#switcher .button {
  width: 100px;
  float: left;
  text-align: center;
  margin: 10px;
  padding: 10px;
  background-color: #fff;
  border-top: 3px solid #888;
  border-left: 3px solid #888;
  border-bottom: 3px solid #444;
  border-right: 3px solid #444;
}
#header {
  clear: both;
}
.selected{
font-weight:bold;
}
.hover{
cursor:pointer;
}
.hideen{
display:none;
}jquery

解决方案 »

  1.   

    $('#switcher').bind("click.collapse",toggleStyleSwitcher); 
    //这是在第1个里注册的,你放在第1个ready开始 $('#switcher').trigger('click');是没执行到toggleStyleSwitcher,或者你放在第1个ready 尾就行了
      

  2.   


    我早上起来弄了之后,发现只要在你说的那句之后那个地方放都可以的,昨晚写这代码的时候,我自己粗心没看清楚地方了吧。。
    你的意思就是在JQUERY中的.ready()方法中的所有方法执行顺序是从上到下的么?
      

  3.   

       
    我早上起来弄了之后,发现只要在你说的那句之后那个地方放都可以的,昨晚写这代码的时候,我自己粗心没看清楚地方了吧。。
    你的意思就是在JQUERY中的.ready()方法中的所有方法执行顺序是从上到下的么?
    #3 说的对 先 .ready() 先执行