[英]jQuery XPath plugin setting attribute
这个问题与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';
}
}
我想设置选定节点的文本属性,如注释行所示。 当我仅将getAttribute
用于阅读目的时,它就像是一种魅力。 我想给它分配某种不可用的setAttribute
函数。 您的任何帮助将不胜感激。
插件链接: http : //plugins.jquery.com/xpath/
插件版本:0.2.4
编辑:我想我应该共享我的xml的一部分,以便更好地理解。
<?xml version="1.0"?>
<call-script say="Good morning">
<option text="caller asks for blah">
...
...
...
</option>
<option text="caller requests blah">
...
...
...
<option text="caller would like to blah">
...
...
...
</option>
</call-script>
您已经有了jQuery,因此,如果您使用jQuery的.attr()
函数,该函数会效率更高一点,该函数根据参数同时充当getter和setter的双重职责。 $.each()
函数也可以在这里使用。
重写示例:
$.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');
这解决了我的问题。 对不起,麻烦您了。
教程链接: http : //www.w3schools.com/xml/met_element_setattribute.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.