【问题标题】:jquery clone with HTML template带有 HTML 模板的 jquery 克隆
【发布时间】:2012-06-28 01:08:45
【问题描述】:

我看过一些关于 jQuery 模板的教程,据我所知,这些教程已经过时了,所以我正在尝试使用 .Clone,当我只显示一个结果时,它可以正常工作,但是当我想显示时整个结果列表都不起作用,我敢肯定是因为我的 jQuery 不好。我想克隆整个 .template 类,从响应数组的每个成员中填充它,然后将每个新克隆的模板添加到 #fillresultsdiv ,如果有人可以帮助找出我做错了什么

这里是 jQuery:

                 success: function (msg) {
                    var events = [];
                    var obj = $.parseJSON(msg.d);
                    $(obj.res).each(function () {
                        var newRow = $('.template').clone()
                        newRow.$('.day').text($(this).attr('day')),
                            newRow.$('.dayofweek').text($(this).attr('dayofweek')),
                            newRow.$('.month').text($(this).attr('month')),
                            newRow.$('.title').text($(this).attr('title')),
                            newRow.$('.time').text($(this).attr('time')),
                        newRow.$('.venue').text($(this).attr('venue')),
                        newRow.$('.description').text($(this).attr('description'))
                        $('#fillresultsdiv').append(newRow);

这是一个示例响应:

   d: "{"res":[{"day":"26","dayofweek":"Tue","month":"Jun","title":"Glen Hansard","venue":"Vic Theatre","time":"7:00 PM","ticketurl":"http://seatgeek.com/glen-hansard-t

这是我的模板 HTML:

    <div class="Template">
      <div class="accordian_head1">
        <div class="date_container">
          <a class="day"></a><br/><br/>
          <a class="dayofweek"></a><br/>
          <a class="month"></a>
        </div>
        <div class="title_container">
          <a class="title">Title</a>
          <a class="venue"><br/></a><a class="time"></a>
        </div>
        <div class="links">
          <a href=" + dr(36).ToString() + ?aid=854">Buy Tickets</a><br/>
          <a href="javascript:void(0)" onclick="fnAddToCalendar({ 'eventID' : '  dr(0).ToString() + '});">Watch</a><br/>
        <a href="#">Email</a><br/>
        <a href=""Calendar.aspx"">Calendar</a><br/>
    </div>
  </div>
  <div class="accordian_body1new"><a class="description"></a>
  </div>

这就是#fillresultsdiv 的全部内容

     <div id="fillresultsdiv"></div> 

【问题讨论】:

  • 你的#fillresultsdiv在html中的什么位置?

标签: jquery asp.net html clone


【解决方案1】:
// Parse the entire string, because `msg` is not an object,
// so you can't use `msg.d`
var obj = $.parseJSON( msg );

$( obj.d.res ).each( function() {
    var newRow = $( '.template' ).clone();

    // Now loop through the object
    for ( var prop in this ) {
        if ( this.hasOwnProperty( prop ) ) {

            // Lucky for you, the keys match the classes :)
            $( '.' + prop, newRow ).text( this[ prop ] );
        }
    }
    $( '#fillresultsdiv' ).append( newRow );
} );

但是您绝对应该使用DocumentFragment 来加快 DOM 操作并立即附加所有内容。因为请记住:DOM 很慢

var obj = $.parseJSON( msg );

// Create a DocumentFragment
var el = document.createDocumentFragment();
$( obj.d.res ).each( function() {
    var newRow = $( '.template' ).clone();
    for ( var prop in this ) {
        if ( this.hasOwnProperty( prop ) ) {
            $( '.' + prop, newRow ).text( this[ prop ] );
        }
    }

    // Append to the DocumentFragment
    $( el ).append( newRow );
} );

// And append the DocumentFragment to the DIV
$( '#fillresultsdiv' ).append( el );

【讨论】:

  • 这看起来是一种更好的方法,但我无法让它工作,它正在通过 if (this.hasOwnProperty(prop)) { 并且 prop 设置为正确的属性,但之后没有任何内容被添加到 '#fillresultsdiv' - 即使 $( '.' + this, newRow ).text( this[ prop ] );什么也没做,我应该添加黑色模板,这里什么都没有添加
  • @ScottSelby 是的,我更正了使用$( '.' + prop ) 而不是$( '.' + this ),我的错。 this 不是字符串,而是对象,这就是它失败的原因。
  • 好的,我有点让它工作了,出于某种原因,尽管它一遍又一遍地显示相同的结果,我仔细检查 JSON,每个事件只收到一次,但在这个循环中 - 我第一个结果显示一次,第二个显示两次,第三个显示 4 次,第四个显示 8 次,第五个显示 16 次,它变成了一个奇怪的二进制数学问题
  • 呃?你复制了这段代码吗?你有什么改变吗?听起来是个奇怪的问题。
  • 是的,我复制并粘贴了它,上面没有循环的答案也会发生这种情况,所以我认为这是 $( obj.res ).each( function( ) {
【解决方案2】:

试试这个:

          success: function (msg) {
                var events = [];
                var obj = $.parseJSON(msg.d);
                $(obj.res).each(function () {
                    var newRow = $('.template').clone();
                    newRow.find('.day').text($(this).attr('day'));
                    newRow.find('.dayofweek').text($(this).attr('dayofweek'));
                    newRow.find('.month').text($(this).attr('month'));
                    newRow.find('.title').text($(this).attr('title'));
                    newRow.find('.time').text($(this).attr('time'));
                    newRow.find('.venue').text($(this).attr('venue'));
                    newRow.find('.description').text($(this).attr('description'));
                    newRow.appendTo('#fillresultsdiv');
                  }

【讨论】:

  • 没用 - 我不太确定 newRow.$('.day').text($(this).attr('day')) 是正确的 jQuery跨度>
  • @ewein 你是一名开发人员。当您看到如此多的重复时,请尝试对此做点什么:) 此外,使用逗号而不是分号对您的代码没有帮助。
  • 我想给出一个类似于他的方法的答案,以便他更容易理解。是的,逗号必须去掉,直到现在我才注意到它们。
猜你喜欢
  • 2013-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-06
  • 2019-10-19
  • 2010-11-28
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多