使用HTML模板进行jquery克隆

时间:2012-06-26 16:40:08

标签: jquery asp.net html clone

我已经看过一些关于jQuery模板的教程,据我所知,这些教程是outdataed,因此我尝试使用.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> 

2 个答案:

答案 0 :(得分:1)

试试这个:

          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');
                  }

答案 1 :(得分: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 );