简体   繁体   English

如何为我使用 jquery 到 append 一个 div,它还包括它的所有嵌套 div?

[英]How can I use jquery to append a div for me which also includes all of it's nested divs as well?

When a user creates a product, I want that product to be broadcasted to all other users and dynamically added to their screens.当用户创建产品时,我希望将该产品广播给所有其他用户并动态添加到他们的屏幕上。 So far the broadcast aspect works amazingly.到目前为止,广播方面的效果非常好。

But how can I dynamically add in this '.product' class, as well as all of a nested divs in an easy way?但是如何以简单的方式动态添加这个“.product”class,以及所有嵌套的 div? At the moment the only thing I can think of is copying and pasting all of it's divs in a jquery variable and adding it that way- there must be an easier way.目前我唯一能想到的是将所有的 div 复制并粘贴到 jquery 变量中并以这种方式添加 - 必须有更简单的方法。

Here is where products are first loaded in when the page loads这是页面加载时首次加载产品的位置

<div class="product" id="{{$product->id}}">
                            <div class="product-image"><img src="/imgs/products/{{$product->type_id}}.png"></div>
                            <div class="product-content">
                                <div class="product-title" id="product-title">
                                    {{ strtoupper(\App\ProductType::where('id', $product->type_id)->first()->name)}}
                                </div>
                                <div class="product-price">PRICE PER UNIT: <div class="price-value" id="price">{{$product->price}}</div> EXENS</div>
                                <br/>
                                QUANTITY: <div class="quantity-value" id="quantity">{{$product->quantity_available}}</div>
                                @if(strpos(\App\Group::where('id', $player->group_id)->first()->options, "\"showName\":true") !== false)
                                    <br/>
                                    SELLER: <div class="seller" id="seller">{{\App\User::where('id',$product->seller_id)->first()->name}}</div>
                                @endif
                                <br/>
                                PRICE: <div class="total-price" id="total-price">{{$product->price * $product->quantity_available}}</div>
                                <form class="buy-product-form"  action="/UoE/buy-product/{{$product->id}}" method="POST">
                                    {{csrf_field()}}
                                    <button class="pull-right btn btn-primary">BUY NOW</button>
                                </form>
                            </div>
                     </div>

When the event is received the only way I can think of doing it as:收到事件后,我能想到的唯一方法是:

var productToAdd="<div class='buy-product-form'><div id='price'></div> " +
                    "" +
                    "" + //insert a massive string here containing all the other aforementioned sub-divs
                    "" + //And populate with json data
                    "" +
                    "</div>";

                $('.content').append(productToAdd);

My solution was to take the entire code posted in the question and do make it as a one big HTML tag.我的解决方案是获取问题中发布的整个代码,并将其作为一个大的 HTML 标记。 That way my JS function can append the page with a HTML product div and it will already be bound with the necessary event listeners.这样我的 JS function 可以 append 页面与 HTML 产品 div 并且它已经与必要的事件侦听器绑定。

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

相关问题 如何在某些父div中附加这些嵌套的div - How do I append these nested divs in some parent div 如何防止嵌套在另一个也使用onclick的元素上的链接上的click事件? - How I can prevent the click event on a link which is nested on another element which also use onclick? 如何在jQuery中将div附加到特定的div? - how to append divs to a specific div in jquery? 如何在按键上获取div的内容,使其也包含在该按键上输入的文本? - How do I get the content of the div on a key press so that it also includes the text which was entered on that keypress? jQuery:如何选择所有特定于div的类并将div中的所有内容附加到新的div - jQuery: How to select all divs specific class and append everything inside the div to a new div 我如何使用/将Pnotify附加到div - How can i use / append pnotify to a div 如何在jQuery中使用“追加”创建多个DIV? - How I can create multiple DIVs with ’append’ in jQuery? 如何在嵌套数组上使用include()函数? - how can I use includes() function on nested arrays? 如何设置包含名称空间以及具有已设置名称空间的主机属性 - How can I set the host property which includes a namespace, as well as have a set namespace jQuery:如何将事件委托与添加到嵌套div的类一起使用? - jQuery: How can I use event delegation with classes added to nested divs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM