Вопрос

While using masonary example, we can create this code to generate new masonary divs.

            function getItemElement() {
              var elem = document.createElement('div');
              var wRand = Math.random();
              var hRand = Math.random();
              var widthClass = wRand > 0.92 ? 'w4' : wRand > 0.8 ? 'w3' : wRand > 0.6 ? 'w2' : '';
              var heightClass = hRand > 0.85 ? 'h4' : hRand > 0.6 ? 'h3' : hRand > 0.35 ? 'h2' : '';
              elem.className = 'item ' + widthClass + ' ' + heightClass;
              return elem;
            }

            $( function() {
              var $container = $('.masonry').masonry({
                columnWidth: 60
              });

              $('#append-button').on( 'click', function() {
                var elems = [ getItemElement(), getItemElement(), getItemElement() ];
                $container.append( elems ).masonry( 'appended', elems );
              });
            });

This code generates this div with random heights and widths

     <div class="item"></div>

I want to generate this code, using javascript, please help me in this, i'm very weak in JS:

             <div class="product desat">
                <div class="saleTag">
                    <div class="saleText">Sale</div>
                    <div class="trunImg"><img src="img/saleTagTrunImg.png" alt=""></div>
                </div>
                <div class="entry-media">
                    <img src="images/jewelry/280128_238089139552597_218540758174102_904483_8113368_o.jpg" alt="" class="lazyLoad thumb desat-ie" />
                    <div class="hover">
                        <a href="product.html" class="entry-url"></a>
                        <ul class="icons unstyled">
                            <li>
                                <a href="images/women/skirt/430041-0014_1.jpg" class="circle" data-toggle="lightbox">
                                    <span>View Detail</span>
                                 </a>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="entry-main arrow_box">
                    <h5 class="entry-title">
                        <a href="product.html">Neckless</a>
                    </h5>
                    <div class="entry-price">
                        <s class="entry-discount">$ 35.00</s>
                        <strong class="accent-color price">$ 25.00</strong>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
Это было полезно?

Решение

I recommend using JQuery Templates.. https://github.com/BorisMoore/jquery-tmpl

very nice plugin that will give you functionality you need the way you are using with Arrays of Inputs..

Other than that, you're right there - adding dynamically these div using .append or .after;

I suggest you try this, then expand on it when you have it working basically..

$('#yourContainerDiv').append('<div>Div Inside Selected Div</div>'); 

or

$('#yourDivs:last').after('<div>New Div</div>')

Thanks, JFIT

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top