2014-12-28 50 views
0

我为朋友建立了一个电子商务网站,使用javascript和html,以及almostfreespeech托管服务。我已经确保正确加载html页面,并且有没有错误(在控制台和实际页面上)。我也尝试过使用嵌入在html中的JavaScript和2个独立的文件,但都不起作用。我正在使用也没有外部库。我添加了页面加载时的警报,但没有显示出来。页面构造函数通过在body标签上设置的onload事件连接到页面。我用Firefox,谷歌浏览器和Internet Explorer进行了试用,但都无效。当我将控件的创建移动到html中时,控件在那里(意味着html正在工作),但警报仍然没有显示(表示JavaScript被忽略或onload事件是不被解雇)。以下是代码:Javascript not running almostfreespeech

<!DOCTYPE HTML> 

<html> 
<head> 
    <title>GetLost</title> 
    <script type="text/javascript"> 
     function loadcontrols() 
    { 
     var items = ["Green cuff", "Red cuff"]; 
     var prices = ["20.00", "30.00"]; 
     var colors = ["Green", "Red"]; 
     var urls = ["http://media-cache-ak0.pinimg.com/736x/0f/f8/11/0ff811addad9b0165263eb73ba9806f0.jpg", "http://www.opalona.com/images/produits/big/big_2049-3.jpg"]; 
     var controls = []; 
     for(var i = 0; i < items.length; i++) 
     { 
      var item = new loaditem(items[i], prices[i], colors[i], urls[i]); 
      controls.concat(item); 
     } 
     alert("All items loaded.") 
    } 
    function loaditem(name, value, color, imageUrl) 
    { 
     this.name = name; 
     this.value = value; 
     this.color = color; 
     this.imageUrl = imageUrl; 
     var container = document.CreateElement("div"); 
     container.style.height = "300px"; 
     container.style.width = "200px"; 
     container.style.backgroundColor = color; 
     scroller.appendChild(container); 
     var image = document.CreateElement("img"); 
     image.style.height = "220px"; 
     image.style.witdh = "180px"; 
     image.setAttribute("src", imageUrl); 
     container.appendChild(image); 
     var name = document.CreateElement("h4"); 
     name.innerHTML = name + " for $" + value; 
     container.appendChild(name); 
     alert("The product " + name + "has been loaded.") 
    } 
    </script> 
</head> 
<body onload="loadcontrols"> 
    <h1><b>Choose a product, and click on it to open it.</b></h1> 
    <div style="overflow-x: scroll" name="scroller" height="310px" width="650px"></div> 
</body> 
</html> 

回答

3

onload不带函数名称;它需要运行一些JavaScript代码。如果你只有一行JavaScript代码只引用一个变量:

loadcontrols 

然后,好吧,没有任何反应。如果你想打电话,你需要括号:

loadcontrols()