简体   繁体   中英

“Object doesn't support property or method” when I try to call plugin

I'm working on a function to do some XML processing. I'm trying to add a plugin that will let me perform XPath queries from jQuery objects.

function GetVersionCollection_FromLiveHistory(itemid)
{
    (function($) {
        $.xpath = function(exp, ctxt) {
            var item, coll = [],
                result = document.evaluate(exp, ctxt || document, null, 5, null);

            while (item = result.iterateNext())
                coll.push(item);

            return $(coll);
        }
    })(jQuery);

    url = "https://someSite.com/sites/xyz/_vti_bin/owssvr.dll?Cmd=Display&XMLDATA=TRUE&List={LISTGUID}&View={VIEWGUID}&IncludeVersions=TRUE&FilterField1=ID&FilterValue1=" + itemid
    returnedXml = null;
    var output=$.get(url,function(returnedXml){
        elVersions=$.parseXML(returnedXml);
        var result={};
        $(elVersions).xpath("//z:row").each(function(index,element){
            versionNumber=$(this).attr('ows__UIVersionString');
            result[versionNumber]=$(this);
            return result;
        });
    },"xml");
    return output;
}

When I call it, the browser returns an error that seems to indicate that xpath has not been successfully added as a jQuery plugin, when it reaches the $(elVersions).xpath("//z:row")... line.

IE:

Object doesn't support property or method 'xpath'

Chrome:

Uncaught TypeError: $(...).xpath is not a function

Probably you are getting this error because you are not creating a jQuery plugin but adding a function to the jQuery object.

You need to assign the function to $.fn and it will be available just like any other jQuery object method.

$.fn.xpath = function(exp, ctxt) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM