代码之家  ›  专栏  ›  技术社区  ›  Jack Roscoe

不确定如何设计使用XML创建HTML对象的JavaScript/jQuery功能

  •  0
  • Jack Roscoe  · 技术社区  · 14 年前

    XML文档中的主“C”节点都有一个type属性,根据我要运行的类型,函数将使用分配给特定“C”节点的其他属性创建一个新的html对象。

    目前,我有一个 for 循环,从XML中提取每个“C”节点及其属性(例如,宽度、高度、x、y)。

    也在里面 对于 if 语句,该语句检查正在处理的当前“C”节点的“type”属性,并根据类型运行不同的函数,该函数随后将使用从XML中提取的属性创建一个新的HTML对象。

    问题是同一类型的“C”节点可能不止一个,因此,例如,当我创建的函数在检测到“type=1”的“C”节点时将运行时,我不能使用 var p = document.createElement('p') '因为如果循环中稍后出现相同类型的'C'节点,它将冲突并用刚刚创建的变量覆盖该元素。

    我真的不知道该怎么做?

    这是我的全部剧本。如果您需要我详细说明任何部分,请询问,我相信这不是用最好的方式写的:

        var arrayIds = new Array();
    $(document).ready(function(){
      $.ajax({
        type: "GET",
        url: "question.xml",
        dataType: "xml",
        success: function(xml)
        {
                      $(xml).find("C").each(function(){
                            arrayIds.push($(this).attr('ID'));
                      });
    
    
                      var svgTag = document.createElement('SVG');
                      // Create question type objects
                      function ctyp3(x,y,width,height,baC)
                      {
                          alert('test');
                          var r = document.createElement('rect');
                          r.x = x;
                          r.y = y;
                          r.width = width;
                          r.height = height;
                          r.fillcolor = baC;
                          svgTag.appendChild(r);
                      }
    
                      // Extract question data from XML
                      var questions = [];
                      for (j=0; j<arrayIds.length; j++)
                      { 
                        $(xml).find("C[ID='" + arrayIds[j] + "']").each(function(){
                            // pass values
                            questions[j] = {
                                typ: $(this).attr('typ'),
                                width: $(this).find("I").attr('wid'),
                                height: $(this).find("I").attr('hei'),
                                x: $(this).find("I").attr('x'),
                                y: $(this).find("I").attr('x'),
                                baC: $(this).find("I").attr('baC'),
                                boC: $(this).find("I").attr('boC'),
                                boW: $(this).find("I").attr('boW')
                            }
    
                            alert($(this).attr('typ'));
    
                            if ($(this).attr('typ') == '3')
                            {
                                ctyp3(x,y,width,height,baC);
                                // alert('pass');
                            } else {
                                // Add here
                                // alert('fail');
                            }
                        });
                      }
        }
      });
    });
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   jessegavin    14 年前

    我的示例在jQuery中使用$.each()函数,并将元素添加到 <body> p .

    即使作者在我的代码示例写完后发布了他们的代码示例,我还是会把它放在这里,以防其他人从中受益。

    在jsFiddler上查看: http://jsfiddle.net/gKN4V/

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Untitled Document</title>
    <script type="text/javascript" 
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript">
      var xmlString = '<root>'
      + '<C type="1" x="25" y="30" text="My cool div" />'
      + '<C type="2" x="50" y="75" text="My other div" />'
      + '<C type="1" x="100" y="10" text="My fun div" />'
      + '<C type="2" x="150" y="150" text="My awesome div" />'
      + '</root>';
    
    $(function() {
      var xml = $(xmlString);
      $("C", xml).each(function(i,o) {
        var node = $(o);
    
        switch(node.attr("type")) {
    
          case "1" :
            $("<p />", { 
              "class" : "type1",
              css : {
                left :node.attr("x") + "px",
                top : node.attr("y") + "px"
              }
            }).text(node.attr("text")).appendTo("body");
            break;
    
            case "2":
              $("<div />", { 
                "class" : "type2",
                css : {
                  left :node.attr("x") + "px",
                  top : node.attr("y") + "px"
                }
              }).text(node.attr("text")).appendTo("body");
                                break;
          }
    
        });
      });
    
    </script>
    <style type="text/css">
      .type1 {
        position: absolute;
        border: solid 1px gray;
        font: normal 12px Arial, Helvetica, sans-serif;
      }
      .type2 {
        position: absolute;
        border: solid 1px green;
        font: bold 12px Arial, Helvetica, sans-serif;
        color: green;
      }
    </style>
    </head>
    <body>
    </body>
    </html>
    
        2
  •  0
  •   Tomalak    14 年前

    我不能用这个 'var p = document.createElement('p')' 因为如果 'C' 相同类型的节点稍后在循环中出现,它将冲突并覆盖该元素

    只是不要使用固定的变量名。让循环将元素添加到数组中:

    var elementList = [];
    var Cs = xml.getElementByTagName("C");
    for (var i=0; i<Cs.length; i++) {
      elementList.push( whateverYourFunctionIsThatCreatesHtmlNodes(Cs[i]) );
    }