簡體   English   中英

包括一個javascript文件?

[英]Including a javascript file?

我有一點麻煩,包括一個javascript文件。 我的頁面上有以下代碼塊,我想將其移動到名為cart.js的單獨文件中。 問題是,每當我將所有腳本移動到該文件時,它就會停止在我的頁面上工作。 我已經嘗試將整個代碼塊包裝在准備好的文檔上但是沒有用。 我不知道如何包含這個。

編輯:由於看到控制台的建議,我發現了我的錯誤。 事實證明,在cart.js中調用jquery導致了這個問題。

current_fin = "none";
current_mat = "Pine";
current_col = "Red";
current_size = "36";
jQuery(document).ready(function($) {
  $("#dropdownthree").hide();
});

// Pass the current selection into a variable to use.
function getMaterial() {// function checks material and if plastic hides/shows boxes
  var mat = document.getElementById("dropdownone");
  current_mat = mat.options[mat.selectedIndex].text;
  if (current_mat == "Plastic") {
    var col = document.getElementById("dropdownthree");
    current_fin = col.options[col.selectedIndex].text;
    $("#dropdowntwo").hide();
    $("#dropdownthree").show();
  } else {
    var fin = document.getElementById("dropdowntwo");
    current_fin = fin.options[fin.selectedIndex].text;
    $("#dropdownthree").hide();
    $("#dropdowntwo").show();
  }
  $.post('php/productajaxtemp.php', {
    ourMaterial: current_mat,
    ourFinish: current_fin,
    ourSize: current_size
  }, function (data) {
    $("#price").html(data).show();
  });
}

function getFinish() {
  var fin = document.getElementById("dropdowntwo");
  current_fin = fin.options[fin.selectedIndex].text;
  $.post('php/productajaxtemp.php', {
    ourMaterial: current_mat,
    ourFinish: current_fin,
    ourSize: current_size
  }, function (data) {
    $("#price").html(data).show();
  });
}

function getColor() {
  var col = document.getElementById("dropdownthree");
  current_col = col.options[col.selectedIndex].text;
  $.post('php/productajaxtemp.php', {
    ourMaterial: current_mat,
    ourFinish: current_col,
    ourSize: current_size
  }, function (data) {
    $("#price").html(data).show();
  });
}

function getSize() {
  var sz = document.getElementById("dropdownsize");
  current_size = sz.options[sz.selectedIndex].text;
  $.post('php/productajaxtemp.php', {
    ourMaterial: current_mat,
    ourFinish: current_col,
    ourSize: current_size
  }, function (data) {
    $("#price").html(data).show();
  });
  getMaterial();
}

關聯的HTML是

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

<form action="cart.php" id="form" method="POST">
  <select name="size" id="dropdownsize" onchange="getSize()">
    <option>36</option>
    <option>48</option>
    <option>60</option>
    <option>72</option>
  </select>
  <select name="material" id="dropdownone" onchange="getMaterial()">
    <option>Pine</option>
    <option>Oak</option>
    <option>Walnut</option>
    <option>Plastic</option>
  </select>
  <select name="finish" id="dropdowntwo" onchange="getFinish()">
    <option>None</option>
    <option>Finished</option>
  </select>
  <select name="color" id="dropdownthree" onchange="getColor()">
    <option>Painted Red</option>
    <option>Painted Green</option>
    <option>Painted Blue</option>
    <option>Painted yellow</option>
    <option>Painted white</option>
    <option>Primer white</option>
  </select>
  <input type="submit" value="Add To Cart">
</form>

根據您使用的JS腳本,您不能只是將JS代碼粘貼到文件中。

嘗試在您的文件中將JS代碼包含在這樣的匿名函數中:

(function() {
    //Your JS
})();

通過這種方式,JS代碼將在文件加載后執行並且可以使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM