我的递归函数正在崩溃,我找不到原因。我正在尝试为json编写一个解析器,以便使用准系统JavaScript,并且似乎无法包装我的头以解释为什么我的函数无法正常工作。任何人都可以借我一只手吗?JSON CSS解析器
在CSS中你会得到属于你的东西解决这样的
#nav li{
some style here
}
试图操纵JavaScript中的同样的事情只有它不工作的特定元素的子元素。我的代码到目前为止简单的选择器工作正常。
var json = {
"body" : {
"background": "#303030",
"color": "#FFFFFF",
"font-family": "Arial, Helvetica, sans-serif"
},
"#nav": {
"li": {
"display": "inline-block",
"list-styles": "none",
"margin-right": "10px",
"padding": "10px, 10px"
}
}
}
// How to use:
// json is an obj
// body is a selector
// background is a property belong to a selector
// #303030 is a value for the property
//apply css changes to document without writing any css
//by default the first values are
// selector = background
// parent = document
// object = json
function styleApply(selector, parent, object){
var element, onSelector, signal;
//checks the first character of the selector and grabs element from page depending on what type of element it is
switch(selector[0]){
case ".":
element= parent.getElementsByClassName(selector.substring(1));
newParent = element[0]; //checks to see what is in the first index of the obtained element
break;
case "#":
element= parent.getElementById(selector.substring(1));
newParent = element;
break;
default:
element= parent.getElementsByTagName(selector);
newParent = element[0];
break;
}
//only works if there is actually an element in the page corresponding with the selector
if(newParent != null){
//loops through all elements with the same selector or in the case of id just does one loop
for(var i=0; i<element.length; i++){
//loops through all properties in the selector
for (var property in object[selector]){
//grabs the associated value with the selector could be string or object
var value= object[selector][property];
//if it is a string it is a style, if it is an object, it is targeting the elements inside the current selector only
if(typeof(value) === "string"){
element[i].style.setProperty(property, value);
}else{
/*I am breaking my code right here*/
//reusing the same function, this time selector is equal to property for the case of nav, it is the element 'li'
//newParent is the element who is a parent of the current element (e.g 'nav' is parent of 'li')
//value is the new object that is replacing json,
styleApply(property, newParent, value); //since value is an object it did not pass the string test and now the new object is value
/*Problem ends here */
}
}
}
}
}
my code for looping over all the values in json
for (var selector in json){
styleApply(selector, document, json);
}
'getElementById'返回一个元素,而不是一个集合。你需要将它包装在一个数组中。 – Barmar 2013-04-23 03:04:39
当你递归地调用你的函数时,你需要传递'element [i]'作为新的父类,而不是'newParent'。 – Barmar 2013-04-23 03:11:19
YAY非常感谢Barmar,我设法缩短了我的代码并获得了继承者的工作!并感谢xbonez查看我的工作。我做了一个额外的更改是在我的价值=对象[选择] 我需要回去一个参数来复制我的json的结构 – 2013-04-23 04:14:10