亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何讓放在body前面的js代碼運行

如何讓放在body前面的js代碼運行

心寧安心 2016-06-03 16:03:17
下面的代碼,js是放在body的底部的,我想放在</style>的下面,也就是body前面運行,但是顯示結果是失敗的,要怎么修改,才能放在頂部,或者放在單獨的js文件中,謝謝!<!Doctype html><html>?? ?<head>?? ??? ?<meta charset="utf-8" />?? ??? ?<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />?? ??? ?<title>調試</title>?? ??? ?<style>?? ??? ??? ?*{margin:0; padding:0; color:black;}?? ??? ??? ?ul{list-style:none;}?? ??? ??? ?a{text-decoration:none;}?? ??? ??? ?a:hover{color:orange;}?? ??? ??? ?#articleList{width:400px; border:5px solid gray;}?? ??? ??? ?#articleTitle{height:50px; line-height:50px; font-size:24px; text-align:center; border-bottom:1px solid gray;}?? ??? ??? ?#articleContent{width:350px; height:125px; margin:10px 25px; overflow:hidden;}?? ??? ??? ?#articleContent ul li a{width:200px; height:25px; line-height:25px; display:inline-block; padding-left:15px; overflow:hidden;}?? ??? ??? ?#articleContent ul li span{float:right; color:gray;}?? ??? ?</style>?? ?</head>?? ?<body>?? ??? ?<div id="articleList">?? ??? ??? ?<h3 id="articleTitle">最近更新文章</h3>?? ??? ??? ?<div id="articleContent">?? ??? ??? ??? ?<ul id="newList">?? ??? ??? ??? ??? ?<li><a href="#">1.做對自己有意義的事</a><span>2016-05-28</span></li>?? ??? ??? ???????? <li><a href="#">2.關于CSS選擇器繼承</a><span>2016-05-30</span></li>?? ??? ??? ???????? <li><a href="#">3.自己動手,豐衣足食</a><span>2016-06-01</span></li>?? ??? ??? ???????? <li><a href="#">4.論inline-block</a><span>2016-06-02</span></li>?? ??? ??? ???????? <li><a href="#">5.更多正在編寫中……</a><span>2016-06-02</span></li>?? ??? ??? ??? ?</ul>?? ??? ??? ??? ?<ul id="newListTemp"></ul>?? ??? ??? ?</div>?? ??? ?</div>?? ???? <script>?? ??? ??? ?var area = document.getElementById("articleContent");?? ??? ??? ?var newList = document.getElementById("newList");?? ??? ??? ?var newListTemp = document.getElementById("newListTemp");?? ??? ??? ?var speed = 50;?? ??? ??? ?area.scrollTop = 0;?? ??? ??? ?newListTemp.innerHTML = newList.innerHTML;?? ??? ??? ??? ??? ??? ?function scrollUp(){?? ??? ??? ??? ?if(area.scrollTop >= newList.scrollHeight){?? ??? ??? ??? ??? ?area.scrollTop = 0;?? ??? ??? ??? ?}?? ??? ??? ??? ?else{?? ??? ??? ??? ??? ?area.scrollTop ++;?? ??? ??? ??? ?}?? ??? ??? ?}?? ??? ??? ?var myScroll = setInterval("scrollUp()", speed);?? ??? ??? ?area.onmouseover = function(){?? ??? ??? ??? ?clearInterval(myScroll);?? ??? ??? ?}?? ??? ??? ?area.onmouseout = function(){?? ??? ??? ??? ?myScroll = setInterval("scrollUp()", speed);?? ??? ??? ?}?? ??? ?</script>?? ?</body></html>
查看完整描述

2 回答

已采納
?
LHammer

TA貢獻9條經驗 獲得超3個贊

再試試

<!Doctype html>

<html>

? ? <head>

? ? ? ? <meta charset="utf-8" />

? ? ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />

? ? ? ? <title>調試</title>

? ? ? ? <style>

? ? ? ? ? ? *{margin:0; padding:0; color:black;}

? ? ? ? ? ? ul{list-style:none;}

? ? ? ? ? ? a{text-decoration:none;}

? ? ? ? ? ? a:hover{color:orange;}

? ? ? ? ? ? #articleList{width:400px; border:5px solid gray;}

? ? ? ? ? ? #articleTitle{height:50px; line-height:50px; font-size:24px; text-align:center; border-bottom:1px solid gray;}

? ? ? ? ? ? #articleContent{width:350px; height:125px; margin:10px 25px; overflow:hidden;}

? ? ? ? ? ? #articleContent ul li a{width:200px; height:25px; line-height:25px; display:inline-block; padding-left:15px; overflow:hidden;}

? ? ? ? ? ? #articleContent ul li span{float:right; color:gray;}

? ? ? ? </style>

? ? ? ? <script>

? ? ? ? ? ? window.onload = function(){

? ? ? ? ? ? ? ? var area = document.getElementById("articleContent");

? ? ? ? ? ? ? ? var newList = document.getElementById("newList");

? ? ? ? ? ? ? ? var newListTemp = document.getElementById("newListTemp");

? ? ? ? ? ? ? ? var speed = 50;

? ? ? ? ? ? ? ? area.scrollTop = 0;

? ? ? ? ? ? ? ? newListTemp.innerHTML = newList.innerHTML;

? ? ? ? ? ? ? ? var myScroll = setInterval(scrollUp, speed);

? ? ? ? ? ? ? ? area.onmouseover = function(){

? ? ? ? ? ? ? ? ? ? clearInterval(myScroll);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? area.onmouseout = function(){

? ? ? ? ? ? ? ? ? ? myScroll = setInterval(scrollUp, speed);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? function scrollUp(){

? ? ? ? ? ? ? ? ? ? if(area.scrollTop >= newList.scrollHeight){

? ? ? ? ? ? ? ? ? ? ? ? area.scrollTop = 0;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? else{

? ? ? ? ? ? ? ? ? ? ? ? area.scrollTop ++;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }?

? ? ? ? ? ? }

? ? ? ? </script>?

? ? </head>

? ? <body>

? ? ? ? <div id="articleList">

? ? ? ? ? ? <h3 id="articleTitle">最近更新文章</h3>

? ? ? ? ? ? <div id="articleContent">

? ? ? ? ? ? ? ? <ul id="newList">

? ? ? ? ? ? ? ? ? ? <li><a href="#">1.做對自己有意義的事</a><span>2016-05-28</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">2.關于CSS選擇器繼承</a><span>2016-05-30</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">3.自己動手,豐衣足食</a><span>2016-06-01</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">4.論inline-block</a><span>2016-06-02</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">5.更多正在編寫中……</a><span>2016-06-02</span></li>

? ? ? ? ? ? ? ? </ul>

? ? ? ? ? ? ? ? <ul id="newListTemp"></ul>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? </body>

</html>


查看完整回答
反對 回復 2016-06-06
?
LHammer

TA貢獻9條經驗 獲得超3個贊

js中可以用window onload() ?代表頁面全部加載完成之后在執行之下的代碼

<!Doctype html>

<html>

? ? <head>

? ? ? ? <meta charset="utf-8" />

? ? ? ? <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />

? ? ? ? <title>調試</title>

? ? ? ? <style>

? ? ? ? ? ? *{margin:0; padding:0; color:black;}

? ? ? ? ? ? ul{list-style:none;}

? ? ? ? ? ? a{text-decoration:none;}

? ? ? ? ? ? a:hover{color:orange;}

? ? ? ? ? ? #articleList{width:400px; border:5px solid gray;}

? ? ? ? ? ? #articleTitle{height:50px; line-height:50px; font-size:24px; text-align:center; border-bottom:1px solid gray;}

? ? ? ? ? ? #articleContent{width:350px; height:125px; margin:10px 25px; overflow:hidden;}

? ? ? ? ? ? #articleContent ul li a{width:200px; height:25px; line-height:25px; display:inline-block; padding-left:15px; overflow:hidden;}

? ? ? ? ? ? #articleContent ul li span{float:right; color:gray;}

? ? ? ? </style>

? ? ? ? ?<script>

? ? ? ? ?window.onload = function(){

? ? ? ? ? var area = document.getElementById("articleContent");

? ? ? ? ? ? var newList = document.getElementById("newList");

? ? ? ? ? ? var newListTemp = document.getElementById("newListTemp");

? ? ? ? ? ? var speed = 50;

? ? ? ? ? ? area.scrollTop = 0;

? ? ? ? ? ? newListTemp.innerHTML = newList.innerHTML;

? ? ? ? ? ??

? ? ? ? ? ? function scrollUp(){

? ? ? ? ? ? ? ? if(area.scrollTop >= newList.scrollHeight){

? ? ? ? ? ? ? ? ? ? area.scrollTop = 0;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else{

? ? ? ? ? ? ? ? ? ? area.scrollTop ++;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? var myScroll = setInterval("scrollUp()", speed);

? ? ? ? ? ? area.onmouseover = function(){

? ? ? ? ? ? ? ? clearInterval(myScroll);

? ? ? ? ? ? }

? ? ? ? ? ? area.onmouseout = function(){

? ? ? ? ? ? ? ? myScroll = setInterval("scrollUp()", speed);

? ? ? ? ? ? }

? ? ? ? } ?

? ? ? ? </script>

? ? </head>

? ? <body>

? ? ? ? <div id="articleList">

? ? ? ? ? ? <h3 id="articleTitle">最近更新文章</h3>

? ? ? ? ? ? <div id="articleContent">

? ? ? ? ? ? ? ? <ul id="newList">

? ? ? ? ? ? ? ? ? ? <li><a href="#">1.做對自己有意義的事</a><span>2016-05-28</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">2.關于CSS選擇器繼承</a><span>2016-05-30</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">3.自己動手,豐衣足食</a><span>2016-06-01</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">4.論inline-block</a><span>2016-06-02</span></li>

? ? ? ? ? ? ? ? ? ? <li><a href="#">5.更多正在編寫中……</a><span>2016-06-02</span></li>

? ? ? ? ? ? ? ? </ul>

? ? ? ? ? ? ? ? <ul id="newListTemp"></ul>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? </body>

</html>


查看完整回答
反對 回復 2016-06-03
  • 心寧安心
    心寧安心
    你運行過行了?我剛才又試了一遍,表示不行,這個我一開始想過,也試過,能不能試成功了再回我,大神0.0
  • 心寧安心
    心寧安心
    錯誤碼: Uncaught ReferenceError: scrollUp is not defined 5 Uncaught ReferenceError: scrollUp is not defined(anonymous function) @ VM52:1 1662 console messages are not shown. 604 Uncaught ReferenceError: scrollUp is not defined
  • LHammer
    LHammer
    兄弟呀~ 我看你寫的題目還以為你放在body后是可以順利運行的~ 下次把題目寫明白咯~ 你的問題出在定時器setInterval中,正確的格式setInterval(code,millisec) 第一個參數為函數代碼(如果調用函數直接寫函數名即可 不要加引號 ),第二個參數為時間間隔
  • 2 回答
  • 1 關注
  • 3114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號