[英]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.