简体   繁体   English

使用javascript变量动态填充XML数据标记

[英]Dynamically populate markup with XML data using javascript variables

I'm on a .NET 4.0 environment using jQuery and Visual Studio to write everything, but neither of these facts should matter that much except how to populate the XML data. 我在使用jQuery和Visual Studio编写所有内容的.NET 4.0环境中,但是除了如何填充XML数据外,这两个事实都没有那么重要。

I'm starting out with this script which has a lot of markup in it. 我从该脚本开始,其中包含很多标记。 My task is to get the markup out of the script and to populate the the XML data into the markup in its proper place. 我的任务是从脚本中删除标记,然后将XML数据填充到标记中的适当位置。 I'm trying to keep the presentation and behavior layers as separate as possible by doing this. 我正在尝试通过这样做使表示层和行为层尽可能分开。

function addIcons(IconType) {
    var li ="";
    var onclickMethod="";
    for (var item in hashObject) {
        var thumbNail = item.split("_");
        if (thumbNail[0] == IconType +"ThumbNail") {
            var imagePath = baseUrlThumbNailImages + hashObject[item];

            li = li + "<li  onclick=IconClick('" + IconType +"',"+ thumbNail[1] + ")><img src=\"" + imagePath + "\" alt=\"" + IconType + " shape\"></li>\n";
        }
    }
    $("#" + IconType + "ThumbNailShapes").append(li);
}

Here's example markup I want as final result: 这是我想要作为最终结果的示例标记:

<ul>
  <li onclick="IconClick('Item',1)">
    <img src="/images/image_1.png" alt="Item shape" />
  </li>
  <li onclick="IconClick('Item',2)">
    <img src="/images/image_2.png" alt="Item shape" />
  </li>
  <li onclick="IconClick('Item',3)">
    <img src="/images/image_3.png" alt="Item shape" />
  </li>
</ul>

While I know I need to take out the line of code that starts li = li + ... , I'm also not familiar enough with OOP to understand how to write a for loop to populate the markup. 虽然我知道我需要取出以li = li + ...开头的代码行,但是我对OOP也不十分了解,无法理解如何编写for循环来填充标记。

So there's two factors that I am not sure how to code: 因此,我不确定有两个因素如何编码:

  1. The blank markup - does the markup need variables to populate it or should javascript do this automatically? 空白标记-标记是否需要变量来填充它?或者javascript应该自动执行此操作?
  2. The javascript - I don't know how to recode the javascript to find each list item and image tag to populate the various data. javascript-我不知道如何重新编码javascript以找到每个列表项和图像标记以填充各种数据。

Do I need the markup to be blank like this, where all the variable data is not there yet? 我是否需要这样的标记为空白,其中所有变量数据都还不存在?

<ul id="IconThumbnailShapes">
  <li onclick="">
    <img src="" alt="shape" />
  </li>
  <li onclick="">
    <img src="" alt="shape" />
  </li>
  <li onclick="">
    <img src="" alt="shape" />
  </li>
</ul>

I appreciate the insight and help. 感谢您的见解和帮助。

I would do it like this. 我会这样做。

Let's suppose we have a container DIV#foo where all this code has to reside. 假设我们有一个必须存放所有这些代码的容器DIV#foo。

<div id="foo"></div>

We then do this: 然后,我们这样做:

$(function() {
   // DOM ready

   $('#foo').on('click', '#IconThumbnailShapes li', function() {
       // Do whatever you have to do with icon
       // Second argument there '#IconThu... li' means the event will be trigerred 
       // as soon as you add your icons, but they don't have to be there 
       // right now.
   });

   function addIcons(iconType) {
       var s = '<ul id="IconThumbnailShapes">'
       var l = '<li><img src="$SRC" alt="$ICON shape" /></li>'
       var data = [{...}, {...}, {...}];
       var len = data.length;

       var datum;

       for (var i = len; i; i--) {
          datum = data[len - i];
          s += l.replace('$SRC', datum.src).replace('$ICON', iconType);
       }

       s += '</ul>';

       $('#foo').html(s);       
   }

});

So, we do it like this, because we don't want to fire jQuery stuff in the for loop. 因此,我们这样做是因为,我们不想在for循环中触发jQuery。 That kills performance. 这会降低性能。 Instead we set up a deferred even handler for click on the conainer DIV#foo, and we shove the whole bunch of markup into the container as a string. 相反,我们设置了一个延迟的偶数处理程序来单击conainer DIV#foo,然后将整个标记串作为字符串推送到容器中。

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

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