首先让我先谈谈我对javascript,特别是JQuery
的了解不多我在页面上加载了一些javascript和Jquery时遇到了一些问题。
1)我在JS小提琴http://jsfiddle.net/Gilera/mT9pV/1/上创建了一些代码,时区转换器有一些javascript,而hide / show div的滑动则有JQuery函数。
使用jsfiddle onDomready时代码运行正常,并显示隐藏div工作的时间和滑动。但是当使用onLoad时,隐藏的div工作但不是时区转换器。知道如何在js小提琴上使用onload模式时如何运行?
2)此外,如果我在浏览器中编译代码和测试网站,我得到的相反是Times加载但不是隐藏的divs点击它们。那我怎么可能改变chan2.js脚本运行可能作为onDomready或者我需要在上面添加一个脚本来找到Jquery Libary?
我为te代码的冗长帖子道歉,但这对我来说是全新的,任何帮助都将不胜感激。
以下是使用
的代码HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="style2.css" rel="stylesheet" type="text/css" />
</head>
<div class="schedule"><div class="event"><ul class="guides"><li class="icon"><img src="" alt="" width="26" height="27" class="icon"/></li><li class="time"><span data-utc="9:05"></span></li><li class="game">Team A vs Team B</li></ul></div><div class="place"><ul class="venue"><li class="field">Field A</li></ul></div></div>
<div class="schedule"><div class="event"><ul class="guides"><li class="icon"><img src="" alt="" width="26" height="27" class="icon"/></li><li class="time"><span data-utc="9:05"></span></li><li class="game">player A vs Player B</li></ul></div><div class="place"><ul class="venue"><li class="field">Court 3</li></ul></div></div>
<div id='out'></div>
<script type='text/javascript' src='times2.js'></script>
<script type='text/javascript' src='chans2.js'></script>
<body>
</body>
</html>
CSS style2.css
@charset "utf-8";
.event {
width: 600px;
height: 38px
}
.place{
display: none;
width: 590px;
height: 38px;
text-align: center;
font-size: 12px;
font-weight: bold;
color: #EB1D2D;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
ul.guides {
width: 570px;
height: 34px;
list-style: none;
display: block;
background-color: #D1E5FD;
border-style: solid;
border-width: 1px;
border-radius: 2px;
border-color: black;
border-spacing: 5px;
padding-top:1px;
border-radius:5px
}
ul.guides a, ul.guides a:visted, ul.guides a:link {
display: block;
text-decoration: none;
background-color: #8090AB;
color: black;
}
ul.guides a:hover, ul.guides a:active, ul.guides a:focus {
background-color: #FFFFFF;
}
li.icon {
display: inline-block;
width: 24px;
height: 24px;
padding-left: 10px;
}
img.icon{
display:inline-block;
padding-top:3px;
}
li.time{
display:inline-block;
text-align:center;
font-size: 12px;
width: 70px;
padding-left: 5px;
color: #0000FF;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
li.game{
display: inline-block;
text-align:center;
font-size: 12px;
padding-left: 10px;
background-color: #D1E5FD;
text-decoration: none;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
ul.guides a, ul.nav a:visted{
display: block;
text-decoration: none;
background-color: #8090AB;
color: #000;
}
ul.guides a:hover, ul.guides a:active, ul.guides a:focus{
background-color: #6F7D94;
color: #000;
}
ul.venue {
width: 550px;
height: 34px;
list-style: none;
display: block;
background-color: #D1E5FD;
border-style: solid;
border-width: 1px;
border-radius: 2px;
border-color: black;
border-spacing: 5px;
padding-top:1px;
border-radius:5px
}
li.field{
width: 150px;
display: inline-block;
text-align:center;
font-size: 12px;
padding-left: 10px;
background-color: #D1E5FD;
text-decoration: none;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
Javascript times.js
window.onload = init;
function init(){
DisplayTimes();
}
function DisplayTimes(){
//legal formats: 1/10-13:00 for date and time
// : 13:00 for time - presumes utc date is same as local date
var dd = new Date();
var list = document.getElementsByTagName('span');
var mon, date, hr, min;
for (var i=0 ; i<list.length ; i++){
if (list[i].hasAttribute('data-utc')){
var str = list[i].getAttribute('data-utc');
if(str.indexOf('/') < 0){
mon = dd.getMonth()+1;
date = dd.getDate();
hr = str.substring(0,str.indexOf(':'));
}else{
mon = str.substring(0,str.indexOf('/'));
date = str.substring(str.indexOf('/')+1,str.indexOf('-'));
hr = str.substring(str.indexOf('-')+1,str.indexOf(':'));
}
min = str.substring(str.indexOf(':')+1);
dd.setUTCDate(mon);//date of month
dd.setUTCHours(hr); //24hour hour
dd.setUTCMinutes(min); //minutes
dd.setUTCSeconds(0); //seconds
var h = leadzero( dd.getHours() );
var m = leadzero( dd.getMinutes() );
var s = leadzero( dd.getSeconds() );
list[i].innerHTML += ' '+ h +':'+ m;
}
}
}
function leadzero(n){
var str1 = n.toString();
if(str1.length < 2){
str1 = '0'+ str1;
}
return str1;
}
Jquery chans2.js
$(".event").click(function(){
//hide all rrshow
$(".place").hide();
//show only required rrshow
$(this).parent().find(".place").show();
});
谢谢
编辑:抱歉发布错误代码,已将chan2.js更改为使用答案 0 :(得分:0)
在times.js
,
而不是代码
window.onload = init;
function init(){
DisplayTimes();
}
使用
$(function(){
DisplayTimes();
}
答案 1 :(得分:0)
您的CSS存在问题,您的html正文元素位于&lt; body&gt;&lt; / body&gt;之外标签,我改变了所有内容,以便在传递给$(document).ready()的单个函数中加载。以下内容适用于我的浏览器:
HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>onLoad</title>
<link href="style2.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="schedule">
<div class="event">
<ul class="guides">
<li class="icon"><img src="" alt="" width="26" height="27" class="icon"/></li>
<li class="time"><span data-utc="9:05"></span></li>
<li class="game">Team A vs Team B</li>
</ul>
</div>
<div class="place">
<ul class="venue">
<li class="field">Field A</li>
</ul>
</div>
</div>
<div class="schedule">
<div class="event">
<ul class="guides">
<li class="icon"><img src="" alt="" width="26" height="27" class="icon"/></li>
<li class="time"><span data-utc="9:05"></span></li>
<li class="game">player A vs Player B</li>
</ul>
</div>
<div class="place">
<ul class="venue">
<li class="field">Court 3</li>
</ul>
</div>
</div>
<div id='out'></div>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script type='text/javascript' src='onLoad.js'></script>
</body>
</html>
JavaScript的:
$(document).ready(function () {
$(".event").click(function () {
//hide all rrshow
$(".place").hide();
//show only required rrshow
$(this).parent().find(".place").show();
});
function DisplayTimes() {
//legal formats: 1/10-13:00 for date and time
// : 13:00 for time - presumes utc date is same as local date
var dd = new Date();
var list = document.getElementsByTagName('span');
var mon, date, hr, min;
for (var i = 0; i < list.length; i++) {
if (list[i].hasAttribute('data-utc')) {
var str = list[i].getAttribute('data-utc');
if (str.indexOf('/') < 0) {
mon = dd.getMonth() + 1;
date = dd.getDate();
hr = str.substring(0, str.indexOf(':'));
} else {
mon = str.substring(0, str.indexOf('/'));
date = str.substring(str.indexOf('/') + 1, str.indexOf('-'));
hr = str.substring(str.indexOf('-') + 1, str.indexOf(':'));
}
min = str.substring(str.indexOf(':') + 1);
dd.setUTCDate(mon); //date of month
dd.setUTCHours(hr); //24hour hour
dd.setUTCMinutes(+min); //minutes
dd.setUTCSeconds(0); //seconds
var h = leadzero(dd.getHours());
var m = leadzero(dd.getMinutes());
var s = leadzero(dd.getSeconds());
list[i].innerHTML += ' ' + h + ':' + m;
}
}
}
function leadzero(n) {
var str1 = n.toString();
if (str1.length < 2) {
str1 = '0' + str1;
}
return str1;
}
DisplayTimes();
});
CSS:
@charset "utf-8";
.event {
width: 600px;
height: 38px
}
.place{
display: none;
width: 590px;
height: 38px;
text-align: center;
font-size: 12px;
font-weight: bold;
color: #EB1D2D;
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
}
ul.guides {
width: 570px;
height: 34px;
list-style: none;
display: block;
background-color: #D1E5FD;
border-style: solid;
border-width: 1px;
border-color: black;
border-spacing: 5px;
padding-top:1px;
border-radius:5px
}
ul.guides a, ul.guides a:visited, ul.guides a:link {
display: block;
text-decoration: none;
background-color: #8090AB;
color: black;
}
ul.guides a:hover, ul.guides a:active, ul.guides a:focus {
background-color: #FFFFFF;
}
li.icon {
display: inline-block;
width: 24px;
height: 24px;
padding-left: 10px;
}
img.icon{
display:inline-block;
padding-top:3px;
}
li.time{
display:inline-block;
text-align:center;
font-size: 12px;
width: 70px;
padding-left: 5px;
color: #0000FF;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
li.game{
display: inline-block;
text-align:center;
font-size: 12px;
padding-left: 10px;
background-color: #D1E5FD;
text-decoration: none;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}
ul.guides a, ul.nav a:visited{
display: block;
text-decoration: none;
background-color: #8090AB;
color: #000;
}
ul.guides a:hover, ul.guides a:active, ul.guides a:focus{
background-color: #6F7D94;
color: #000;
}
ul.venue {
width: 550px;
height: 34px;
list-style: none;
display: block;
background-color: #D1E5FD;
border-style: solid;
border-width: 1px;
border-color: black;
border-spacing: 5px;
padding-top:1px;
border-radius:5px
}
li.field{
width: 150px;
display: inline-block;
text-align:center;
font-size: 12px;
padding-left: 10px;
background-color: #D1E5FD;
text-decoration: none;
font-family: Tahoma, Geneva, sans-serif;
font-weight: bold;
}