我在Rails3上正确加载了jQuery,并且$(document).ready()
事件从未运行过。我之所以知道这一点,是因为我尝试通过Google Chrome的Javascript控制台在$(document).ready();
中手动运行代码,结果运行得很好。
我一点也不确定出了什么问题,所以我想问问StackOverFlow
请注意,ERB/HTML的质量还不好。我只是在学习这个。
这是ERB:
<html>
<head>
<style type="text/css">
#Agenda
{
background-color: #36175E;
width: 960px;
margin: 0 auto;
text-align: center;
}
#assignment
{
background: green;
width: 600px;
height: 500px;
float: right;
margin: auto;
}
#accordion
{
margin: auto;
width: 350px;
height: 500px;
float: left;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('#accordion').accordion();
SetupAccordionItems();
});
//This is probably not the best way to do it
$('li[id]').click(function(e)
{
SetupAssignment(this.id);
});
function SetupAssignment(Assignment_ID)
{
if (Assignment_ID != null)
{
$('#ShowAssignmentInstructions').fadeOut('slow');
document.getElementbyID(ShowAssignmentInstructions).visibility = "hidden";
var Assignment = "BAEGLS";
document.getElementbyID(Assignment_Name).InnerText = Assignment.name;
document.getElementbyID(Due_Date).InnerText = Assignment.due_date;
if (Assignment.image == null)
{
Assignment.image = Assignment.Unit.Image;
}
document.getElementbyID(Assignment_Image).src = Assignment.Image;
//Resets contents of the paragraph describing the assignment
document.getElementByID(basic_assignment_info).InnerText = null;
document.getElementbyID(basic_assignment_info).InnerText = "This assignment is worth " + Assignment.DefaultMaximumPoints + "points.";
document.getElementbyID(assignment).style.visibility = "visibility";
document.getElementbyID(Assignment_Status).src = Assignment.Status;
}
else
{
$('#ShowAssignmentInstructions').fadeIn('slow');
}
}
function SetupAccordionItems()
{
var AssignmentsDueNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextSchoolDay) %>;
var AssignmentsDueNextNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextNextSchoolDay) %>;
var AssignmentsDueNextNextNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextNextNextSchoolDay) %>;
for (assignment in AssignmensDueNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInOneSchoolDay");
}
for (Assignment in AssignmentsDueNextNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInTwoSchoolDays");
}
for (Assignment in AssignmentsDueNextNextNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInThreeSchoolDays");
}
}
function AppendAccordionItem(Item, ID)
{
$("<li>", {id:Item.ID, text:Item.Name}).appendTo(ID);
}
</script>
</head>
<body>
<div id="Agenda">
<h1 id="Agenda_Title">Agenda</h1>
<div class="accordion" id="accordion">
<h3 id="TomorrowsDate"><%= @nextSchoolDay %></h3>
<ul id="DueInOneSchoolDay">
</ul>
<h3 id="DueDayAfterTomorrow_Title"><%= @nextNextSchoolDay %></h3>
<div>
<ul id="DueInTwoSchoolDays">
</ul>
</div>
<h3 id="DueDayAfterDayAfterTomorrrow_Title"><%= @nextNextNextSchoolDay %></h3>
<ul id="DueInThreeSchoolDays">
</ul>
</div>
<div id="assignment" display="hidden">
<h2 id="Assignment_Name"><center>Assignment Name</center></h2>
<h3 id="Due_Date"><center>Due Date</center></h3>
<img id="Assignment_Image" src="#"/>
<br>
<button id="Start_Assignment">Start Assignment</button>
<p id="basic_assignment_info">
</p>
<img id="Assignment_Status" src="#"/>
</div>
</div>
</body>
</html>
这是ERB 输出的HTML
<html>
<head>
<style type="text/css">
#Agenda
{
background-color: #36175E;
width: 960px;
margin: 0 auto;
text-align: center;
}
#assignment
{
background: green;
width: 600px;
height: 500px;
float: right;
margin: auto;
}
#accordion
{
margin: auto;
width: 350px;
height: 500px;
float: left;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('#accordion').accordion();
SetupAccordionItems();
});
//This is probably not the best way to do it
$('li[id]').click(function(e)
{
SetupAssignment(this.id);
});
function SetupAssignment(Assignment_ID)
{
if (Assignment_ID != null)
{
$('#ShowAssignmentInstructions').fadeOut('slow');
document.getElementbyID(ShowAssignmentInstructions).visibility = "hidden";
var Assignment = "BAEGLS";
document.getElementbyID(Assignment_Name).InnerText = Assignment.name;
document.getElementbyID(Due_Date).InnerText = Assignment.due_date;
if (Assignment.image == null)
{
Assignment.image = Assignment.Unit.Image;
}
document.getElementbyID(Assignment_Image).src = Assignment.Image;
//Resets contents of the paragraph describing the assignment
document.getElementByID(basic_assignment_info).InnerText = null;
document.getElementbyID(basic_assignment_info).InnerText = "This assignment is worth " + Assignment.DefaultMaximumPoints + "points.";
document.getElementbyID(assignment).style.visibility = "visibility";
document.getElementbyID(Assignment_Status).src = Assignment.Status;
}
else
{
$('#ShowAssignmentInstructions').fadeIn('slow');
}
}
function SetupAccordionItems()
{
var AssignmentsDueNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextSchoolDay) %>;
var AssignmentsDueNextNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextNextSchoolDay) %>;
var AssignmentsDueNextNextNextSchoolDay = <%= GetByDueDateForCurrentUser(@nextNextNextSchoolDay) %>;
for (assignment in AssignmensDueNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInOneSchoolDay");
}
for (Assignment in AssignmentsDueNextNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInTwoSchoolDays");
}
for (Assignment in AssignmentsDueNextNextNextSchoolDay)
{
AppendAccordionItem(Assignment, "DueInThreeSchoolDays");
}
}
function AppendAccordionItem(Item, ID)
{
$("<li>", {id:Item.ID, text:Item.Name}).appendTo(ID);
}
</script>
</head>
<body>
<div id="Agenda">
<h1 id="Agenda_Title">Agenda</h1>
<div class="accordion" id="accordion">
<h3 id="TomorrowsDate"><%= @nextSchoolDay %></h3>
<ul id="DueInOneSchoolDay">
</ul>
<h3 id="DueDayAfterTomorrow_Title"><%= @nextNextSchoolDay %></h3>
<div>
<ul id="DueInTwoSchoolDays">
</ul>
</div>
<h3 id="DueDayAfterDayAfterTomorrrow_Title"><%= @nextNextNextSchoolDay %></h3>
<ul id="DueInThreeSchoolDays">
</ul>
</div>
<div id="assignment" display="hidden">
<h2 id="Assignment_Name"><center>Assignment Name</center></h2>
<h3 id="Due_Date"><center>Due Date</center></h3>
<img id="Assignment_Image" src="#"/>
<br>
<button id="Start_Assignment">Start Assignment</button>
<p id="basic_assignment_info">
</p>
<img id="Assignment_Status" src="#"/>
</div>
</div>
</body>
</html>
首先需要像这样导入jquery文件。
<script type="text/javascript" src="jquery-1.4.1.js"></script>