English 中文(简体)
a basic javascript class and instance using jquery "$(this)" for XML parser
原标题:

I am (slowly) writing an XML parser for some "site definition" files that will drive a website. Many of the elements will be parsed in the same manner and I won t necessarily need to keep the values for each.

The XML

The parser so far

My question is actually pretty simple: How can I use jquery manipulators in an class function? How can I pass $(this)? I know that it sometimes refers to a DOM object and sometimes the jQuery object, but am a bit hazy.

For my function:

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;
            }
        };
    }); 
    }
    });

I want to place

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

in a class, so I can do something like

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

and then use child.label , child.text and child.key... But again, not sure how to use the jquery methods with these... I have more nodes to process and I don t want to keep doing stuff like var label = $(this).get(0).tagName; and then var childLabel = $(this).get(0).tagName;

Thanks.

最佳回答
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 so this works but, I m not sure if it s the best way.

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签