我已经通过此JavaScript成功实现了大多数所需的功能,但是仍然有一个潜在的麻烦错误,我无法弄清楚:我编写的代码会附加所需的HTML,但会创建不必要的重复项。
该代码会多次添加按钮,而只应添加一次。如何停止倍数?
第(li)个项目以ul的形式显示,就像这样的表格(编号是任意的,我仅将其用作参考):
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
$('.videoDemoBtn').on('click', function () {
if ($(this).hasClass('videoPlaying')) {
$(this).removeClass('videoPlaying');
$(this).parent().find('div.categoryDemoVideo').hide().html('');
}
else {
var ProductId = $(this).parent().find('div.ProductImage').attr('data-product');
$(this).addClass('videoPlaying');
$(this).parent().find('div.categoryDemoVideo').show().html('<video id="demoVideo" class="video" preload="auto" autoplay="autoplay" loop="loop" autobuffer="autobuffer" muted="muted" controls="controls" width="100%" height="100%" poster="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.jpg"><source src="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.mp4"><source src="https://store-mixi7d.mybigcommerce.com/content/videos/'+ProductId+'.ogv" type="video/ogg"><p>Your browser does not support this video. Please upgrade your browser!</p></video>');
}
});
$(".Options").each(function checkForVideo(url) {
var ProductCatOpt = $(this);
ProductId = $(this).parent().parent().find('div.ProductImage').attr('data-product');
function ajax1() {
return $.ajax('/content/videos/'+ProductId+'.mp4')
.done(function() {
$(ProductCatOpt).addClass('withVideo');
}).fail(function() {
return;
});
}
$.when(ajax1()).done(function(a1){
$('.withVideo').closest('li').append('<span class="videoDemoBtn"><div class="triangle"></div></span>');
});
});
<ul class="ProductList " style="position: relative; height: 1407px;">
<li class="Odd " style="min-height: 456px; position: absolute; left: 0px; top: 0px;">
<div class="ProductImage QuickView" data-product="296">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- Here in the first listing in the ul with class .Options it creates 2 buttons -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- Here in the first listing in the ul with class .Options it creates 2 buttons -->
</li>
<li class="Even " style="min-height: 456px; position: absolute; left: 310px; top: 0px;">
<div class="ProductImage QuickView" data-product="431">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- While here in the 2nd to last listing in the ul with class .Options it creates 3 buttons -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- While here in the 2nd to last listing in the ul with class .Options it creates 3 buttons -->
</li>
<li class="Odd " style="min-height: 435px; position: absolute; left: 0px; top: 476px;">
<div class="ProductImage QuickView" data-product="389">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
<li class="Even " style="min-height: 435px; position: absolute; left: 310px; top: 476px;">
<div class="ProductImage QuickView" data-product="393">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Choose Options withVideo" title="Choose Options">Choose Options</a></div>
<!-- And yet ere in the last listing in the ul with class .Options it creates just 1 button -->
<span class="videoDemoBtn"><div class="triangle"></div></span>
<!-- And yet ere in the last listing in the ul with class .Options it creates just 1 button -->
</li>
<li class="Odd " style="min-height: 456px; position: absolute; left: 0px; top: 931px;">
<div class="ProductImage QuickView" data-product="428">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
<li class="Even " style="min-height: 456px; position: absolute; left: 310px; top: 931px;">
<div class="ProductImage QuickView" data-product="388">
<div class="categoryDemoVideo"></div>
</div>
<div class="ProductActionAdd" style="display:;">
<a href="#someproduct" class="btn Small icon-Add To Cart" title="Add To Cart">Add To Cart</a></div>
</li>
</ul>
我在这里做了一个JSFiddle,但这没有帮助,因为必须使用ajax检查的视频必须在同一域中才能正常工作,并且ProductID由我使用的平台的PHP动态生成。
我特别希望停止导致附加多个按钮而不是仅附加1个按钮的行为,但是,如果我的代码草率且效率低下,我不会感到惊讶,因此,请随时批评其中的任何部分。
要处理动态元素,您需要绑定到预先存在的元素,并将事件处理委托给该元素。
改变
$('.videoDemoBtn').on('click', function () {
到
$('.ProductList').on('click', '.videoDemoBtn', function () {
[更新]
对于按钮重复的问题,您很可能需要更改
$('.withVideo').closest('li').append('<span class="videoDemoBtn"><div class="triangle"></div></span>');
到
ProductCatOpt.closest('li').append('<span class="videoDemoBtn"><div class="triangle"></div></span>');
这样,您只需将元素附加到相关产品,而不是.withVideo
页面中的所有元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句