Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery XPath插件设置属性_Javascript_Jquery_Xml_Xpath - Fatal编程技术网

Javascript jQuery XPath插件设置属性

Javascript jQuery XPath插件设置属性,javascript,jquery,xml,xpath,Javascript,Jquery,Xml,Xpath,这个问题是关于Sergey Ilinsky的jQuery XPath插件的。我有一个xml文件,我通过这个插件导航它。文件本身并不重要。这个问题更多地与插件的功能有关 var elemList = $(xmlDoc).xpath("call-script/option" + xpathExtension); for (var i = 0; i < elemList.length; i++) { if (elemList[i].getA

这个问题是关于Sergey Ilinsky的jQuery XPath插件的。我有一个xml文件,我通过这个插件导航它。文件本身并不重要。这个问题更多地与插件的功能有关

        var elemList = $(xmlDoc).xpath("call-script/option" + xpathExtension);
        for (var i = 0; i < elemList.length; i++) {
            if (elemList[i].getAttribute('text') != null) {
                alert(elemList[i].getAttribute('text'));
                //elemList[i].getAttribute('text') = 'example';
            }
        }

您已经有了jQuery,因此如果您使用jQuery的
.attr()
函数,可以提高一点效率,该函数根据参数兼作getter和setter。
$。这里也可以使用each()
函数

 elemList[i].setAttribute('text','example');
重写您的样本:

$.each(elemList, function(key, element) {   // iterate over each thing in elemList
    if (element.attr('text') !== null) {   // without 2nd argument, just returns the value
        alert(element.attr('text'));   // same
        element.attr('text', 'example');   // with 2nd argument, sets the value
    }
});

看起来也有一个
setAttribute
函数

 elemList[i].setAttribute('text','example');
这解决了我的问题。对不起打扰你了


教程链接:

我喜欢这种方法。但是,它给出了“element.attr不是函数”错误。我解析的xml不是html。