检查重复项后使用insertbefore()
16lz
2021-01-22
I am using insertbefore() function in JS:
我在JS中使用insertbefore()函数:
$('<div class="col-lg-3" ></div>').insertBefore("div.col-lg-9.col-md-9.col-sm-9.col-xs-12");
But what issue I am facing when there is already exists that particular div div col-lg-3 before "div.col-lg-9.col-md-9.col-sm-9.col-xs-12" after that its always perform insertbefore.
但是,在“div.col-lg-9.col-md-9.col-sm-9.col-xs-12”之前已经存在特定div div col-lg-3时,我面临的问题是什么?它始终执行insertbefore。
I want to check duplicacy and if already its present before same div then avoid insertbefore in that condition on a page.
我想检查重复项,如果已经存在它在同一个div之前,那么在页面上避免insertbefore。
1 个解决方案
#1
2
You can use each()
to loop and use prev()
function to get the element before and check if it is div.col-lg-3
using is()
.
您可以使用each()循环并使用prev()函数来获取元素,并使用is()检查它是否为div.col-lg-3。
$("div.col-lg-9.col-md-9.col-sm-9.col-xs-12").each(function(){
if( !$(this).prev().is("div.col-lg-3") )
$('<div class="col-lg-3" ></div>').insertBefore(this);
});
更多相关文章
- 深入理解javascript函数进阶系列第二篇——函数柯里化
- 在Servlet和HTML页面之间处理函数调用和数据传输的最佳方法是什
- 函数的作用是:在javascript中将时间戳转换为人工日期
- 使用HTML文件中的React调用.js文件中的Javascript函数
- 根据循环中的i改变函数中的参数
- iframe操作、调用父页面元素或js函数
- Javascript构造函数有什么意义?
- 使用单个函数隐藏多个输入
- 如何从控制器内的javascript函数调用角范围函数