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

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

需要幫助在外部 js 文件中定義我的變量

需要幫助在外部 js 文件中定義我的變量

慕田峪7331174 2022-10-27 10:45:03
我正在為我的一個班級項目做一個網站,它沒什么特別的,但我無法定義我的變量,也不知道如何定義,因為我對此比較陌生,我一直在嘗試使用不同的方式來調用變量,但他們不會定義,我不知道為什么我一直在使用括號,所以任何幫助將不勝感激/*variables used in index.html document*/var square = document.getElementById("square")var clickMe = document.getElementById('clickMe');/*What the button is suppose to do*/function doDemo() {      var button = this;    square.style.backgroundColor = "#ff4545";    button.setAttribute("diabled", "true");    setTimeout(clearDemo, 2000, button);}/*removes the attributes when you click the button again*/function clearDemo (button) {    var square = document.getElementById("square");    square.style.backgroundColor = "transparent";    button.removeAttribute("diabled");}clickMe.onclick = doDemo;/* color for webpage*/html{    background: silver;    }/* setting for heading 1 */h1{    color: saddlebrown;    font-family: Helvetica, sans-serif;    text-align: center;    font-size: 55px;}/* settings for heading 2 */h2{    font-size: 30px;    color: #D2691E;    font-family: Helvetica, sans-serif;}/* settings for the introduction line */.intro{    text-align: center;    padding-bottom: 1em;    font-family: arial;    margin: auto;    max-width: 1100px;    line-height: 2;    font-size: 20px;       }/* settings for images */.shake {   display:block;margin-left: auto;    margin-right: auto;    width: 30%;}/* settings for body structure */.prime{    padding-bottom: 1em;    font-family: arial;    margin-right: auto;    max-width: 900px}#square{    width: 100px;    height: 100px;    border: 2px inset gray;    margin-bottom: 1em;}button{    padding: .5em 2em;}
查看完整描述

3 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

現代瀏覽器中最有效的方法是defer在你的腳本標簽中使用屬性標記頭部的標記,這樣你就可以將頭部中的單行代碼更改為:

<script defer type="text/javascript" src="script.js"></script>

基本上這是最好的方法,因為解析就像我們將腳本放在 body 標記的末尾一樣完成,但總體而言,腳本執行之前完成得很好,因為腳本已與 HTML 解析并行下載。Usingdefer給你兩全其美:異步加載,但它會等到 DOM 準備好。

有一篇非常好的文章詳細解釋了每種 javascript 加載技術的效率。在這里。


查看完整回答
反對 回復 2022-10-27
?
鳳凰求蠱

TA貢獻1825條經驗 獲得超4個贊

建議使用 eventListeners ,這將解決問題


此外,您需要確保正確拼寫 - 它是disabled


/*variables used in index.html document*/

let square; 

let clickMe;


/*What the button is suppose to do*/

function doDemo() {

  square.style.backgroundColor = "#ff4545";

  clickMe.setAttribute("disabled", "true");

  setTimeout(clearDemo, 2000);

}

/*removes the attributes when you click the button again*/

function clearDemo(button) {

  square.style.backgroundColor = "transparent";

  clickMe.removeAttribute("disabled");

}


window.addEventListener("load", function() {

  square = document.getElementById("square")

  clickMe = document.getElementById('clickMe');

  clickMe.addEventListener("click", doDemo);

});

/* color for webpage*/


html {

  background: silver;

}



/* setting for heading 1 */


h1 {

  color: saddlebrown;

  font-family: Helvetica, sans-serif;

  text-align: center;

  font-size: 55px;

}



/* settings for heading 2 */


h2 {

  font-size: 30px;

  color: #D2691E;

  font-family: Helvetica, sans-serif;

}



/* settings for the introduction line */


.intro {

  text-align: center;

  padding-bottom: 1em;

  font-family: arial;

  margin: auto;

  max-width: 1100px;

  line-height: 2;

  font-size: 20px;

}



/* settings for images */


.shake {

  display: block;

  margin-left: auto;

  margin-right: auto;

  width: 30%;

}



/* settings for body structure */


.prime {

  padding-bottom: 1em;

  font-family: arial;

  margin-right: auto;

  max-width: 900px

}


#square {

  width: 100px;

  height: 100px;

  border: 2px inset gray;

  margin-bottom: 1em;

}


button {

  padding: .5em 2em;

}

<!DOCTYPE html>

<!-- will need to make java, and css files and link them-->

<html>

<!--website title and attributes-->


<head>

  <title>Milkshakes</title>

  <meta charset="UTF-8">

  <link rel="stylesheet" href="main.css">

  <script type="text/javascript" src="script.js"></script>

</head>

<!-- make seprate class for each elment here-->


<body>

  <h1>Milkshakes</h1>

  <!-- the div class for our images-->


  <div class="shake">

    <img src="images/milkshake_image_1.jpg" alt="image of a chocolate milkshake">

  </div>


  <!-- our div class for the introduction line-->

  <div class="intro">

    <p>When the sun is out and the heat is beating down on you there is nothing as good as a nice cool milkshake. they come in many varietys</p>

  </div>


  <!-- div class for our body structure-->

  <div class="prime">

    <h2>What is a milkshake</h2>


    <!-- intro sentence-->

    <p>A milkshake it is a nice cold drink made with milk and a flavor of your choosing and ice cream mixed until its nice and creamy.</p>


    <!-- start of body: section 1-->

    <h2>History of the milkshake</h2>



    <p>It was during the (1870's-1900's) there was 2 diffrent types of shakes a normal shake and a milkshake but they were very different from each other. the first milkshake made used whole milk, vanilla or stawberry and nutmeg and then you span it for

      2 to 3 minutes, you get a frothy beverage that was very nice to drink. it is also belived to have started drive throughs.</p>


    <p> the way milkshakes use to be mixed was with a mechanical shakers they were built soley for mixing milkshakes and they took 2 to 3 minutes to mix a shake and the original flavours were chocolate, vanilla pineapple and coffee they were the main ones

      you would find there are others but they werent as common.</p>


    <!-- new section begins: section 2-->

    <h2>The best flavor of milkshake</h2>


    <p>now that is a question not so easily answered as everone has different opinons on which flavor is the best my favorite is chocolate or vanilla depends on my mood but for someone else that could be mint or strawberry so thats all on you as i can not

      tell you which is the best.</p>


    <!-- new section begins: section 3-->

    <h2>What is the perfect thickness</h2>


    <p>The 3 most common thicknesses you will hear about is thin,regular or thick.

    </p>


    <!-- are list that we will make collapsable through java-->

    <ol>

      <li>thin its more of a liquid and light.</li>

      <li>regular its a nice consistency of light and thick.</li>

      <li>thick which is my personal favorite it more along the lines of just being ice cream and its much heavier then the other 2.</li>

    </ol>


    <!-- sources for our milkshake stuff-->

    <h2>Sources</h2>

    <p>

      the art of drink:

      <a href="https://www.artofdrink.com/soda/history-of-the-milkshake">https://www.artofdrink.com/soda/history-of-the-milkshake</a></p>

  </div>


  <!-- our 2nd image-->

  <div class="shake">

    <img src="images/milkshake_image-2.jpg" width="400px" alt="2nd image of a milkshake with different topings">

  </div>


  <div id="square"></div>


  <button type="button" id="clickMe">Click Me</button>




</body>



</html>


查看完整回答
反對 回復 2022-10-27
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

您正試圖在 HTML 元素被解析到內存之前對其進行定位和引用。將您的<script>引用移至結束body標記之前。


在下面的代碼中,我手動插入了您將要引用的代碼,但只要您將script標簽保留在我下面的位置,您就可以刪除 JavaScript 并將 .js 引用添加到script.


/* color for webpage*/

html{

    background: silver;

    }

/* setting for heading 1 */

h1{

    color: saddlebrown;

    font-family: Helvetica, sans-serif;

    text-align: center;

    font-size: 55px;

}

/* settings for heading 2 */

h2{

    font-size: 30px;

    color: #D2691E;

    font-family: Helvetica, sans-serif;

}

/* settings for the introduction line */

.intro{

    text-align: center;

    padding-bottom: 1em;

    font-family: arial;

    margin: auto;

    max-width: 1100px;

    line-height: 2;

    font-size: 20px;

   

    

}

/* settings for images */

.shake {

   display:block;

margin-left: auto;

    margin-right: auto;

    width: 30%;

}

/* settings for body structure */

.prime{

    padding-bottom: 1em;

    font-family: arial;

    margin-right: auto;

    max-width: 900px

}


#square{

    width: 100px;

    height: 100px;

    border: 2px inset gray;

    margin-bottom: 1em;

}



button{

    padding: .5em 2em;

}

<!DOCTYPE html>

<!-- will need to make java, and css files and link them-->

<html>

    <!--website title and attributes-->

<head>

    <title>Milkshakes</title>

    <meta charset="UTF-8">

    <link rel="stylesheet" href="main.css">

    </head>

<!-- make seprate class for each elment here-->

    <body>

        <h1>Milkshakes</h1>

        <!-- the div class for our images-->

        

        <div class="shake">

        <img src="images/milkshake_image_1.jpg" alt="image of a chocolate milkshake">

            </div>

        

        <!-- our div class for the introduction line-->

        <div class="intro">

        <p>When the sun is out and the heat is beating down on you there is nothing as good as a nice cool milkshake. they come in many varietys</p>

        </div>

        

        <!-- div class for our body structure-->

         <div class="prime">

        <h2>What is a milkshake</h2>

        

       <!-- intro sentence-->

        <p>A milkshake it is a nice cold drink made with milk and a flavor of your choosing and ice cream mixed until its nice and creamy.</p>

             

        <!-- start of body: section 1-->

        <h2>History of the milkshake</h2>

       

        

        <p>It was during the (1870's-1900's) there was 2 diffrent types of shakes a normal shake and a milkshake but they were very different from each other. the first milkshake made used whole milk, vanilla or stawberry and nutmeg and then you span it for 2 to 3 minutes, you get a frothy beverage that was very nice to drink. it is also belived to have started drive throughs.</p>

            

        <p> the way milkshakes use to be mixed was with a mechanical shakers they were built soley for mixing milkshakes and they took 2 to 3 minutes to mix a shake and the original flavours were chocolate, vanilla pineapple and coffee they were the main ones you would find there are others but they werent as common.</p>

            

             <!-- new section begins: section 2-->

        <h2>The best flavor of milkshake</h2>

            

            <p>now that is a question not so easily answered as everone has different opinons on which flavor is the best my favorite is chocolate or vanilla depends on my mood but for someone else that could be mint or strawberry so thats all on you as i can not tell you which is the best.</p>

            

             <!-- new section begins: section 3-->

            <h2>What is the perfect thickness</h2>

            

            <p>The 3 most common thicknesses you will hear about is thin,regular or thick. 

            </p>

            

            <!-- are list that we will make collapsable through java-->

            <ol>

            <li>thin its more of a liquid and light.</li>

            <li>regular its a nice consistency of light and thick.</li>    

            <li>thick which is my personal favorite it more along the lines of just being ice cream and its much heavier then the other 2.</li>

            </ol>

            

            <!-- sources for our milkshake stuff-->

            <h2>Sources</h2>

            <p>

                the art of drink:

                <a href="https://www.artofdrink.com/soda/history-of-the-milkshake">https://www.artofdrink.com/soda/history-of-the-milkshake</a></p>

        </div>

        

        <!-- our 2nd image-->

       <div class="shake">

        <img src="images/milkshake_image-2.jpg" width= "400px" alt="2nd image of a milkshake with different topings">

        </div>  

        

        <div id="square"></div>

        

        <button type="button" id="clickMe">Click Me</button>

        

     <script>

/*variables used in index.html document*/

var square = document.getElementById("square")

var clickMe = document.getElementById('clickMe');


/*What the button is suppose to do*/

function doDemo() {  

    var button = this;

    square.style.backgroundColor = "#ff4545";

    button.setAttribute("diabled", "true");

    setTimeout(clearDemo, 2000, button);

}

/*removes the attributes when you click the button again*/

function clearDemo (button) {

    var square = document.getElementById("square");

    square.style.backgroundColor = "transparent";

    button.removeAttribute("diabled");

}


clickMe.onclick = doDemo;     

     </script>       

        

    </body>

    


</html>


查看完整回答
反對 回復 2022-10-27
  • 3 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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