Jquery赢得';不要在循环中指定样式 $(文档).ready(函数(){ $(“堆栈”)。每个(函数(){ style=$(this.parse_attr(); $(this).convert_el().css(样式); }); }); (函数($){ $.fn.parse_attr=函数(){ 如果(this.length==0){ 返回null; } 标记类型=[“类”、“id”、“名称”]; css_hash={}; $.each(此[0]。属性,函数(){ attrib=this.name.split(“:”) if(!($.inArray(属性[0],标记类型>=0)){ if($.isNumeric(attrib[1])){ css_hash[attrib[0]]=attrib[1]+“px”; }否则{ css_hash[attrib[0]]=attrib[1]; } } }); 返回css_散列; }; }(jQuery)); (函数($){ $.fn.convert_el=函数(){ 如果(this.length==0){ 返回null; } Klass=this.prop(“标记名”).toLowerCase(); 标记类型=[“类”、“id”、“名称”]; $.each(此[0]。属性,函数(){ attrib=this.name.split(“:”) if($.inArray(属性[0],标记类型)>=0){ Klass=Klass+“”+attrib[1]。替换(/“/g,”); } }); this.replaceWith($(“”+this.html()+“”)); 归还这个; } }(jQuery));

Jquery赢得';不要在循环中指定样式 $(文档).ready(函数(){ $(“堆栈”)。每个(函数(){ style=$(this.parse_attr(); $(this).convert_el().css(样式); }); }); (函数($){ $.fn.parse_attr=函数(){ 如果(this.length==0){ 返回null; } 标记类型=[“类”、“id”、“名称”]; css_hash={}; $.each(此[0]。属性,函数(){ attrib=this.name.split(“:”) if(!($.inArray(属性[0],标记类型>=0)){ if($.isNumeric(attrib[1])){ css_hash[attrib[0]]=attrib[1]+“px”; }否则{ css_hash[attrib[0]]=attrib[1]; } } }); 返回css_散列; }; }(jQuery)); (函数($){ $.fn.convert_el=函数(){ 如果(this.length==0){ 返回null; } Klass=this.prop(“标记名”).toLowerCase(); 标记类型=[“类”、“id”、“名称”]; $.each(此[0]。属性,函数(){ attrib=this.name.split(“:”) if($.inArray(属性[0],标记类型)>=0){ Klass=Klass+“”+attrib[1]。替换(/“/g,”); } }); this.replaceWith($(“”+this.html()+“”)); 归还这个; } }(jQuery));,jquery,css,Jquery,Css,如果我不迭代“stack”元素,它将应用样式,但由于某种原因,它会在迭代过程中忽略.css(样式)。有什么想法吗 另外,请不要问我为什么要这样做,我只是在尝试通过jquery传递不存在的html标记来动态开发网页的概念性想法,比如 $(document).ready(function(){ $("stack").each(function(){ styles = $(this).parse_attr(); $(this).convert_el().css(styles);

如果我不迭代“stack”元素,它将应用样式,但由于某种原因,它会在迭代过程中忽略.css(样式)。有什么想法吗

另外,请不要问我为什么要这样做,我只是在尝试通过jquery传递不存在的html标记来动态开发网页的概念性想法,比如

$(document).ready(function(){
  $("stack").each(function(){
    styles = $(this).parse_attr();
    $(this).convert_el().css(styles);
  });
});
(function($){
  $.fn.parse_attr = function(){
    if(this.length === 0 ){
        return null;
    }
    tag_types = ["class","id","name"];
    css_hash = {};
    $.each(this[0].attributes,function(){
        attrib = this.name.split(":")
        if( !($.inArray(attrib[0],tag_types) >= 0) ){
            if($.isNumeric(attrib[1])){
                css_hash[attrib[0]] = attrib[1] + "px";
            }else{
                css_hash[attrib[0]] = attrib[1];
            }
        }
    });
    return css_hash;
};
}(jQuery));

(function($){
$.fn.convert_el = function(){
    if(this.length === 0 ){
        return null;
    }
    klasses = this.prop("tagName").toLowerCase();
    tag_types = ["class","id","name"];
    $.each(this[0].attributes,function(){
        attrib = this.name.split(":")
        if($.inArray(attrib[0],tag_types) >= 0 ){
            klasses = klasses + " " + attrib[1].replace(/"/g,"");
        }
    });
    this.replaceWith($("<div class='"+ $.trim(klasses) + "'>" + this.html() + "</div>"));
    return this;
}
}(jQuery));


然后,它应该将这些值解析为一个样式并应用它。

我想问题在于替换。原始元素已被删除,因此您无法更改其样式,但
$.fn.convert\el
仍然返回它

稍微更改一下该函数即可得到所需的结果

<stack class:center margin:100 color:red background-color:#FFF></stack> 
newDiv=$(“”+this.html()+“”)
此。替换为(newDiv);
返回newDiv;

谢谢,这正是我想要的。
newDiv = $("<div class='"+ $.trim(klasses) + "'>" + this.html() + "</div>") 
this.replaceWith(newDiv);
return newDiv;