我的递归函数正在破坏,我无法弄清楚为什么。我正在尝试使用准系统javascript为json编写一个解析器,并且似乎无法解决为什么我的函数不起作用。任何人都可以帮我一把吗?
在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);
}
答案 0 :(得分:0)
这是我读到的关于将json解析为css的简短代码,我想尝试一下
- 选择器指的是你在json中编写的css选择器
- 父开始引用文档,如javascript
中的Document对象模型- object是在
中传递的json对象 如果我误用任何术语,请再次纠正我
function styleApply(selector, parent, object){
var element, onSelector, signal;
switch(selector[0]){
case ".":
element= parent.getElementsByClassName(selector.substring(1));
break;
case "#":
element= [parent.getElementById(selector.substring(1))];
break;
default:
element= parent.getElementsByTagName(selector);
break;
}
if(element[0] != null){
for(var i=0; i<element.length; i++){
for (var property in object[selector]){
var value= object[selector][property];
if(typeof(value) === "string"){
element[i].style.setProperty(property, value);
}else{
styleApply(property, element[i], object[selector]);
}
}
}
}
}
答案 1 :(得分:0)
我碰巧看着json编写一个更清晰的CSS而不是将字符串和数字连接在一起。我没有将样式直接应用于每个元素,而是将它们放入<style>
元素中,这使得在整个页面生命周期中更容易更改。它还能够定位伪元素,例如:之前,我使用过。
无论如何,这是函数:
// convert json to css
function jsonToCss(obj) {
var x, finalStr=''
for (x in obj) {
var y, decStr=''
for (y in obj[x]) {
decStr+= y + ':' + obj[x][y] + ';'
}
finalStr+= x + '{' + decStr + '}'
}
return finalStr
}
由于该函数仅通过两个级别(一个用于选择器,一个用于声明)递归,因此需要按如下方式修改json:
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"
}
}
将其输入函数并获得以下字符串:
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;}
然后您可以将其放入样式元素中。我没有验证函数产生的字符串,看它是否格式正确,但它在我的结尾处工作得很好。