基於Jquery的XML解析器,返回定製的HTML

來源:互聯網
上載者:User

標籤:http   color   java   io   ar   資料   div   cti   代碼   

依據HTML模板返回解析的XML 
依賴jQuery 1.4
?1. [代碼]基於Jquery的xml解析器並返回定製的HTML     
/**
 *  jQuery外掛程式
 *  Author: [email protected]
 *  Date  : 2011-02-16
 *  Params: dom:XMLDocument ,
 *          _default:{
 *              reg:匹配標籤正則,
 *              lc:標籤左結束符,
 *              rc:標籤右結束符,
 *              script:是否包含script,
 *              scriptID:包含script的節點,
 *              tplBox:模板,
 *              tplhtml:資訊條目
 *          }
 *  Return: HTMLDocument
 *  Note  : XML處理外掛程式
 *           
 */
(function($){
    $.fn.extend({
        parseXML:function(dom,option){
            var that=$(dom);
            var html={};
            var tpl=‘‘;
             
            //配置資料
            var _default={
                reg:/\{\w+\}/gi,
                lc:‘{‘,
                rc:‘}‘,
                script:true,
                scriptID:‘script‘,
                tplBox:‘<ul class="indexiframelist skin3">{item}</ul>{more}‘,
                tplhtml:{
                    item:‘<li><a href="{link}"><span class="timer">{pubDate}</span>{title}</a></li>‘,
                    more:‘<div class="moreLink"><a href="{link}">{title}</a></div>‘,
                    script:‘function {funcationname}{{funcationcode}}‘
                }背景音樂http://www.huiyi8.com/bgm/?
            }       
            var option=$.extend(_default,option);
             
            //開始處理tplBody
            $.each(option.tplhtml,function(o,str){
                var rego=str.match(option.reg);
                html[o]=[];
                $(o,that).each(function(){
                    var D=$(this),shtml=str;
                    $.each(rego,function(i,c){
                        shtml=shtml.replace(c,D.find(c.replace(option.lc,‘‘).replace(option.rc,‘‘)).text());
                    }); 
                    html[o].push(shtml);
                });
            }); 
             
            //開始處理tplBox
            var tplHtml=option.tplBox;
            tpl=option.tplBox.match(option.reg);
            $.each(tpl,function(i,c){
                tplHtml=tplHtml.replace(c,html[c.replace(option.lc,‘‘).replace(option.rc,‘‘)].join(‘‘));
            }); 
             
            //向頁面添加script
            if(option.script){          
                var script=document.createElement(‘script‘);
                script.type="text/javascript";
                script.text=html[option.scriptID].join(‘‘);
                $(‘html > head‘).append(script);
            }   
             
            return tplHtml;
        }
    });
})(jQuery);
?

基於Jquery的XML解析器,返回定製的HTML

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.