XML DOM substringData() method
Definition and Usage
The substringData() method extracts a substring from the comment node.
Syntax:
commentNode.substringData(start,length)
parameters | description |
---|---|
start | required. Specifies the position of the first character to be returned. This value starts from 0. |
length | required. Specifies the number of characters in the substring to be returned. |
return value
Returns a string containing from the Comment node start 开始的 length characters.
description
This method returns from the Comment node start 开始的 length Character. This method is useful only when the number of characters in the text contained in the node is greater than the maximum number of characters that can be filled in by the JavaScript implementation of the string. In this case, the JavaScript program cannot directly use the data attribute of the Comment node, but must use a shorter substring of the node text. In practical applications, this situation is unlikely to occur.
Пример
Следующий фрагмент кода использует функцию JavaScript loadXMLDoc() Перенесите XML-файл books_comment.xml Загрузите xmlDoc и верните подстроку из первого узла комментария ("Твердый переплет"):
xmlDoc=loadXMLDoc("books_comment.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
for (i=0;i<x.length;i++)
{
if (x[i].nodeType==8)
{
//Обработка только узлов comment
y=x[i].substringData(10,9);
document.write(x[i].data);
document.write("<br />");
}
}
Вывод кода выше:
(Твердый переплет)
В этом примере мы используем цикл и оператор if для выполнения обработки только для узлов comment. Тип узла comment - 8.
Соответствующие страницы
Референтное руководство DOM XML:CharacterData.substringData()