0

我这里有一些代码可以加载外部页面,当然会替换正文及其内容。这很好,除了 body 标签有一些我想检索的属性,这些属性不是由 .css 嵌入在标签中的。我无法更改页面,因为它们有数千个。任何的意见都将会有帮助。

加载页面的代码:

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

样本主体标签:

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

1 回答 1

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 加载另一个页面并获取正文属性

于 2012-08-24T05:07:05.697 回答