从JSON文件中提取的JavaScript文件不再有效

时间:2015-11-05 05:28:33

标签: javascript json

我昨天有这个工作,但已经做了一些事情来打破它,似乎无法找到它是什么。 JavaScript只是不再创建UL和LI元素。很抱歉,我还是JavaScript的新手(以及一般的编码),所以对我来说调试仍然很困难,尤其是逻辑错误。我尝试在Firefox中检查Firebug,但我只是没有看到错误。

基本上页面应该在左栏显示带有缩略图pics的吉他列表(nav id ="吉他"),然后当你点击一个时,右边的较大列填充较大的图片和关于吉他的文本(JSON文件)。

这是我的HTML源代码和JavaScript:



// Find links for filtering the display
var range = document.querySelectorAll('.range');

for(var link in range){
	range[link].onclick= function(){
		filter(this.dataset.low, this.dataset.high);
	}
}

var myJSON = null;
	
function filter(json,low,high){
	clear("#display");
	var display = document.querySelector("#display");
	for(var key in json){
			//UL
		var ul = document.createElement("ul");		
		var image = createImage(json[key].name, ul);  // pass the name as 'file' to our fucntion of createImage			
		for(var subkey in json[key]){
				//LI
			var li = document.createElement("li");
			var txt = document.createTextNode(subkey +" "+ json[key][subkey]);
				// key = main level of json
				// subkey = properties within each level of an object for JSON
				li.appendChild(txt);
				ul.appendChild(li);
			} // end for subkey
			display.appendChild(ul);
	  } // end for key
} // end filter function


function Guitars(json){
	clear("#guitars");
	var display = document.querySelector("#guitars");
	for(var key in json){
		var ul = document.createElement("ul");		
	  	var li = document.createElement("li");
		li.dataset.key = key; // <li data-key="0" Yoda>
		li.onclick = viewGuitars;
		var image = createImage(json[key].name, li); // pass the name as 'file' to our fucntion of createImage		
		var txt = document.createTextNode(json[key].name);
		li.appendChild(txt);
		ul.appendChild(li);
		display.appendChild(ul);
	} // end for key
} // end filter function

function viewGuitars(){
	var single = myJSON[this.dataset.key]; /* get the single info for a JSON element, this === clicked li element in HTML */
	clear("#display");
	var display = document.querySelector("#display");
		//UL
		//if(low < json[key].cost && json[key].cost <= high){
	var ul = document.createElement("ul");		
	var image = createImage(single.name, ul);  // pass the name as 'file' to our fucntion of createImage
	for(var subkey in single){
				//LI
		var li = document.createElement("li");
		var txt = document.createTextNode(subkey +" "+ single[subkey]);
				// key = main level of json
				// subkey = properties within each level of an object for JSON
			li.appendChild(txt);
			ul.appendChild(li);
		} // end for subkey	
	display.appendChild(ul);
} // end for function


function log(msg){
    console.log(msg);
}

function clear(target){
	var t = document.querySelector(target);
	
	while(t.hasChildNodes()){
		t.removeChild(t.firstChild);	
	}
}

function loadJSON(callback) {   
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open('GET', 'guitars.json', true); // Replace 'my_data' with the path to your file
    xobj.onreadystatechange = function () {
          if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
          }
    };
    xobj.send(null);  
 }
 
 loadJSON(function(response){
	myJSON = JSON.parse(response); 
	filter(myJSON);
	// Build the character list
	 character(myJSON);
 });
 
 
 function createImage(file, parent, w=50){
	 	//set a var to the replaced text
		//log the repalced text var
		var str = file.toLowerCase();  //Look for guitars name in JSON file and make them LowerCase, ex. return back "gibson sg"
		var filename = str.replace(/\s/g, ""); //find blank space (/\s/g | s = space, g = global) and remove it
		
		filename = ("photos/"+filename+".jpg");
		var image = new Image(); // Make a new IMG object
		image.src = filename;
		image.style.width = w+"%";
		image.style.height = "auto";
		
		image.onload = function(){
			log('good ' + file );	
			parent.appendChild(image); //adds the image to the page!
		}
		
		image.onerror = function(){
			log('not able to load ' + filename );	
			//parent.appendChild(image);
		}
 }
 
 
 
 
 
 
 
 
 
 
 
 
&#13;
<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>JSON</title>
     	<style>
		#guitars {
			width:200px;
			float:left;
		}
		#guitars img {display:block}
		
		#guitars li {
			cursor: pointer;
		}
		
		#guitars li:hover {
			background-color: #000;
			color: #FFF;
		}
		
		#display {
			width:800px;
			float:left;
		}
		</style>
        
    </head>
    <body>

        <nav id="guitars">
            Guitar names to appear here
        </nav>

        <section id="display">
            
        </section>
        
        <script src="script.js"></script>
       
    </body>
</html>
&#13;
&#13;
&#13;

...这是我的JSON文件:

&#13;
&#13;
[
  {
    "MAKE":"Gibson",
    "MODEL":"SG",
    "COLOR":"Black",
    "BODY-TYPE":"Solid, 2x pointed horns",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gibson",
    "MODEL":"Les Paul",
    "COLOR":"Gold",
    "BODY-TYPE":"Solid, 1 pointed horn, 1 rounded",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gibson",
    "MODEL":"Firebird",
    "COLOR":"Red",
    "BODY-TYPE":"Solid, \"Z\" shaped w/ rounded horns",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gibson",
    "MODEL":"Explorer",
    "COLOR":"Burgandy",
    "BODY-TYPE":"Solid, \"Z\" shaped w/ pointed horns",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gibson",
    "MODEL":"Flying V",
    "COLOR":"Yellow",
    "BODY-TYPE":"Solid, \"V\" shaped w/ pointed horns",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Fender ",
    "MODEL":"Stratocaster",
    "COLOR":"Aqua Blue",
    "BODY-TYPE":"Solid, w bolt-on neck",
    "WOOD":"Alder, Ash, Poplar (varies)",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Fender ",
    "MODEL":"Telecaster",
    "COLOR":"\"Sunburst\"",
    "BODY-TYPE":"Solid, w bolt-on neck",
    "WOOD":"Alder, Ash, Poplar (varies)",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Fender ",
    "MODEL":"Jaguar",
    "COLOR":"Brown",
    "BODY-TYPE":"Solid, w bolt-on neck",
    "WOOD":"Alder, Ash, Poplar (varies)",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Fender ",
    "MODEL":"Mustang",
    "COLOR":"Bright Red",
    "BODY-TYPE":"Solid, w bolt-on neck",
    "WOOD":"Alder, Ash, Poplar (varies)",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Paul Reed Smith",
    "MODEL":"SE Standard",
    "COLOR":"Silver",
    "BODY-TYPE":"Solid, 2x pointed horns",
    "WOOD":"Maple",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Paul Reed Smith",
    "MODEL":"Dragon",
    "COLOR":"Orange",
    "BODY-TYPE":"Solid, 2x pointed horns",
    "WOOD":"Maple",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Paul Reed Smith",
    "MODEL":"McCarthy",
    "COLOR":"Dark Brown",
    "BODY-TYPE":"Solid, 2x pointed horns",
    "WOOD":"Maple",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gretsch",
    "MODEL":"Jupiter Thunderbird",
    "COLOR":"Black",
    "BODY-TYPE":"Solid, Asymetrical shape",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gretsch",
    "MODEL":"White Falcon",
    "COLOR":"White",
    "BODY-TYPE":"Semi-hollow, 1 pointed horn, 1 rounded",
    "WOOD":"Spruce",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Gretsch",
    "MODEL":"Triple Jet",
    "COLOR":"Copper",
    "BODY-TYPE":"Semi-hollow, 1 pointed horn, 1 rounded",
    "WOOD":"Mahogany",
    "ACOUSITC/ELECTRIC":"Electric"
  },
  {
    "MAKE":"Martin",
    "MODEL":"D-28",
    "COLOR":"Natural wood",
    "BODY-TYPE":"Hollow, \"dreadnought\" design",
    "WOOD":"Spruce",
    "ACOUSITC/ELECTRIC":"Acoustic"
  },
  {
    "MAKE":"Martin",
    "MODEL":"D-18",
    "COLOR":"Natural wood",
    "BODY-TYPE":"Hollow, \"dreadnought\" design",
    "WOOD":"Spruce",
    "ACOUSITC/ELECTRIC":"Acoustic"
  },
  {
    "MAKE":"Taylor",
    "MODEL":"310",
    "COLOR":"Natural wood",
    "BODY-TYPE":"Hollow, \"dreadnought\" design",
    "WOOD":"Rosewood",
    "ACOUSITC/ELECTRIC":"Acoustic"
  },
  {
    "MAKE":"Taylor",
    "MODEL":"360e",
    "COLOR":"Natural wood",
    "BODY-TYPE":"Hollow, \"dreadnought\" design",
    "WOOD":"Rosewood",
    "ACOUSITC/ELECTRIC":"Acoustic"
  }
]
&#13;
&#13;
&#13;

任何帮助非常感谢!!

1 个答案:

答案 0 :(得分:0)

  • 第8行,第1列,不要在循环中创建函数。 这指向看起来各种错误的代码
  • 第7行,第6栏,缺少分号。
  • 第103行,第38栏,'默认参数'仅在ES6中可用(使用esnext选项)。 最有可能导致
  • 第118行,第10栏,缺少分号。
  • 第123行,第10栏,缺少分号。