[英]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.