简体   繁体   English

Javascript XML DOM设置属性为特定元素

[英]Javascript XML DOM Set Attribute to specific element

How do I go about setting an attribute, but only to an element that has a certain value in it? 我该如何设置属性,但只能设置其中具有特定值的元素?

For example: 例如:

<Farm>
<animals>
<type>chicken</type>
<age>1</age>
</animals>

<animals>
<type> Cat</type>
<age>4</age>
</animals>

 <animals>
<type>Cow</type>
<age> 3</age>
</animals>
</farm>

How do I make the javascript file, add an attribute to the element that has a Cow? 如何制作javascript文件,向具有Cow的元素添加属性? Adding the element: name with value: Fred. 添加元素:名称,值:Fred。

I've tried this and it adds the attribute NOT THE VALUE, but it adds it to ALL the elements too. 我已经尝试过了,它添加了属性NOT THE VALUE,但它也将它添加到了所有元素中。

function get_firstchild(n) {
            x = n.firstChild;
            while (x.nodeType != 1) {
                x = x.nextSibling;
            }
            return x;
        }

        xmlDoc = loadXMLDoc("A74.xml");

        x = xmlDoc.documentElement;
        firstNode = get_firstchild(x);

        for (i = 0; i < firstNode.childNodes.length; i++) {
            if (firstNode.childNodes[i].nodeType == 1) {

                document.write("<br>Node Name: " + firstNode.childNodes[i].nodeName + "<br>");
                document.write("Node Type:  " + firstNode.childNodes[i].nodeType + "<br>");
                document.write("Node Value:  " + firstNode.childNodes[i].childNodes[0].nodeValue + "<br>");
                document.write("");
            }
        }


        x=xmlDoc.getElementsByTagName("animals");

        for(i=0;i<x.length;i++)
        {
            x.item(i).setAttribute("name","Fred");
        }

        //Output book title and edition value
        x=xmlDoc.getElementsByTagName("type");
        for (i=0;i<x.length;i++)
        {
            document.write(x[i].childNodes[0].nodeValue);
            document.write(" - name: ");
            document.write(x[i].parentNode.getAttribute('name'));
            document.write("");
        }

If you were looking to actually add a new HTML element you would do something like this: 如果您要实际添加新的HTML元素,则可以执行以下操作:

 //get every element that has the tag name of "type" var types = document.getElementsByTagName("type"); //loop through this array-like object of html. (you do not have access to array.prototype methods so you can use slice or for loop. For less confusion I used a for loop. for (var i = 0; i < types.length; i++){ //if the text inside the HTML is === to cow, insert new HTML after it if (types[i].innerText === "Cow") { types[i].insertAdjacentHTML('afterend', '<name> Fred </name>'); } } 
 <Farm> <animals> <type>chicken</type> <age>1</age> </animals> <animals> <type> Cat</type> <age>4</age> </animals> <animals> <type>Cow</type> <age> 3</age> </animals> </farm> 

And if you wanted to add it as an attribute of the existing element. 并且如果您想将其添加为现有元素的属性。

  //get every element that has the tag name of "type" var types = document.getElementsByTagName("type"); //loop through this array-like object of html. (you do not have access to array.prototype methods so you can use slice or for loop. For less confusion I used a for loop. for (var i = 0; i < types.length; i++){ //if the text inside the HTML is === to cow, insert new HTML after it if (types[i].innerText === "Cow") { types[i].setAttribute("name", "Fred"); } } 
  <Farm> <animals> <type>chicken</type> <age>1</age> </animals> <animals> <type> Cat</type> <age>4</age> </animals> <animals> <type>Cow</type> <age> 3</age> </animals> </farm> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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