Question

Je suis (lentement) à écrire un analyseur XML pour certains fichiers « définition du site » qui stimuleront un site Web. Un grand nombre des éléments sera analysé de la même manière et je n'a pas besoin nécessairement de conserver les valeurs pour chacun.

XML

L'analyseur jusqu'à présent

Ma question est en fait assez simple: Comment puis-je utiliser Manipulateurs jquery dans une fonction de classe? Comment puis-je passer $ (cela)? Je sais qu'il se réfère parfois à un objet DOM et parfois l'objet jQuery, mais je suis un peu brumeux.

Pour ma fonction:

function parseXML(xml) {
    $("book, site", xml).children().each(function() {
        var label = $(this).get(0).tagName;
        var text = $(this).text();
        var key = toCamelCase(label);
        if ((key in siteData) && (text != -1)){
        if (isArray(siteData[key]))
        {   
            $(this).children().each(function (){
                var childLabel = $(this).get(0).tagName;
                var childText = $(this).text();
                var childKey = toCamelCase(childLabel);
                if(isArray(siteData[key][childKey]))
                {
                  siteData[key][childKey].push(childText);  
                }
                else {
                    siteData[key].push(childText);
                }
            });  
        }
        else 
        {
            siteData[key] = text;
            }
        };
    }); 
    }
    });

Je veux placer

var label = $(this).get(0).tagName; var text = $(this).text(); var key = toCamelCase(label);

dans une classe, je peux faire quelque chose comme

var child = new Element(); and var subchild = new Element();

puis utilisez child.label , child.text and child.key ... Mais encore une fois, pas sûr de savoir comment utiliser les méthodes jquery avec ces ... J'ai plusieurs noeuds à traiter et je ne veux pas continuer à faire des choses comme var label = $(this).get(0).tagName; and then var childLabel = $(this).get(0).tagName;

Merci.

Était-ce utile?

La solution

var app = {};
app.element = function(data) {
    return function() {
    var _text = data.get(0).tagName, _label= data.text(), _key = toCamelCase(_label);
        var that = {};
        that.label = function() {
            return _label;
        }
        that.text = function() {
            return _text;
        }
        that.key = function() {
            return _key;
        }
        return that;
    }();
};
app.instanceA = app.element($(this));
document.writeln(app.instanceA.label()); 

Ok, donc cela fonctionne, mais je ne suis pas sûr que ce soit la meilleure façon.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top