2012-08-23 23 views
0

我有一些代码在这里加载一个外部页面,当然取代了正文和它的内容。除了body标签有一些我想要检索的属性,而不是通过.css嵌入标签之外,这很好。我无法更改页面,因为有成千上万的页面。任何的意见都将会有帮助。如何使用jQuery检索和设置身体属性时,不css

代码加载页面:

$(document).ready(function() { 
    $('[id=Page]').load($(JQCurrentPagelink).val(), function() { 
}); 

样品身体标记:

<body background="/Image/ReviewFade02.jpg" leftmargin="20" topmargin="20" marginwidth="20" marginheight="20"> 
+0

对不起,正文属性在这里:background =“/ TracksImage/ReviewFade02.jpg”leftmargin =“20”topmargin =“20”marginwidth =“20”marginheight =“20” –

+0

它可能是浏览器,而不是JQuery这个。看到这里:http://stackoverflow.com/questions/2488839/does-jquery-strip-some-html-elements-from-a-string-when-using-html – jonkroll

+0

即使我必须打两个电话一抢body标签属性然后设置主体,然后使用.load检索文件,我很好,如果没有更好的方法。无论是我还是解析所有.html页面的内容并为每个页面创建一个css页面。 –

回答

0

试试这个:

$.ajax({ 
    url: 'page-to-load.html', 
    success: function(data) { 
     // load the content of the other page into #ajax-div 
     $('#ajax-div').html(data); 
     // a RegEx pattern to grab the opening body tag 
     var pattern=new RegExp("<body.*>"); 
     // grab the opening body tag 
     var bodyOpeningTag = pattern.exec(data)[0]; 
     // remove everything we don't need ('<body ', '>' and quotes) 
     var bodyAttributesString = bodyOpeningTag.replace('<body ', ''); 
     bodyAttributesString = bodyAttributesString.replace('>', ''); 
     bodyAttributesString = bodyAttributesString.replace(/\"/g,''); 
     // split the string into a one dimensional array ('background=/images/about.jpg', 'leftmargin=20' etc) 
     var bodyAttributesArray = bodyAttributesString.split(' '); 
     // split each attribute item into a [attributename, value] array 
     var bodyAttributesArray2d = new Array(); 
     for (var i = 0; i < bodyAttributesArray.length; i++) { 
      bodyAttributesArray2d[i] = bodyAttributesArray[i].split('='); 
     } 
    } 
}); 

演示这里是页:

jQuery - ajax load another page and grab body attributes