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);
});

更多相关文章

  1. 深入理解javascript函数进阶系列第二篇——函数柯里化
  2. 在Servlet和HTML页面之间处理函数调用和数据传输的最佳方法是什
  3. 函数的作用是:在javascript中将时间戳转换为人工日期
  4. 使用HTML文件中的React调用.js文件中的Javascript函数
  5. 根据循环中的i改变函数中的参数
  6. iframe操作、调用父页面元素或js函数
  7. Javascript构造函数有什么意义?
  8. 使用单个函数隐藏多个输入
  9. 如何从控制器内的javascript函数调用角范围函数

随机推荐

  1. 《淘宝客户端 for Android》项目实战 htm
  2. Android实现透明的颜色效果
  3. 三十三、Android给ListView设置分割线Div
  4. Android 5.0 默认水波纹背景属性,可设置
  5. android在RelativeLayout里的TextView或
  6. 利用oom_adj提高Application所在进程的优
  7. 丰富多彩的Android onTouch事件
  8. linux下部署android模拟器
  9. Android:WebView与Javascript交互(相互调用
  10. Android(安卓)Studio 提示Error running