Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用不同的类名包装DIV标记?_Javascript_Jquery_Dom_Parent Child_Jquery Traversing - Fatal编程技术网

Javascript 如何用不同的类名包装DIV标记?

Javascript 如何用不同的类名包装DIV标记?,javascript,jquery,dom,parent-child,jquery-traversing,Javascript,Jquery,Dom,Parent Child,Jquery Traversing,重复: 我在文档中重复了以下HTML块 <!-- first block --> <div class="first"> My first div </div> <div class="second"> My second div </div> <!-- second block --> <div class="first"> My first div </div> <di

重复:

我在文档中重复了以下HTML块

<!-- first block -->
<div class="first">
   My first div
</div>
<div class="second">
   My second div
</div>

<!-- second block -->
<div class="first">
   My first div
</div>
<div class="second">
   My second div
</div>

...

我的第一个div
我的第二个div
我的第一个div
我的第二个div
...
如何使用jQuery包装div以获得如下结果的HTML

<!-- first block -->
<div class="container">
   <div class="first">
      My first div
   </div>    
   <div class="second">
      My second div
   </div>
</div>

<!-- second block -->
<div class="container">
   <div class="first">
      My first div
   </div>    
   <div class="second">
      My second div
   </div>
</div>

...

我的第一个div
我的第二个div
我的第一个div
我的第二个div
...
$('div').wrapAll('');
可以这样做,但这也会包含任何其他div,因此可能:

$('.first, .second').wrapAll('<div class="container" />'); 
$('.first,.second').wrapAll('');

更好。

你很幸运,这正是原因:

在该示例中,我假设div位于一个类为“block”
的容器中

|

如果没有结构性的方法来识别它们,你就必须用其他方法来识别。例如,在这里,我们假设任何时候我们第一次看到
,就应该停止分组:

var current = $();

$(".first, .second").each(function() {
  var $this = $(this);
  if ($this.hasClass('first')) {
    doTheWrap(current);
    current = $();
  }
  current = current.add(this);
});
doTheWrap(current);

function doTheWrap(d) {
  d.wrapAll('<div class="container"></div>');
}

|

否,
wrap
将分别对它们进行包装。(每个都将获得自己的容器div。)wrap()和wrapAll()不起作用:wrap()方法包装每个div,wrapAll()将所有块包装在一起。我需要一个函数来包装每个块…这将是因为你没有明确指定你想要什么,现在已经改变了你的问题!哦我不知道拉帕尔+1是的,我的编辑更改了问题,因此我重新发布了问题,并在jQuery中的一行代码中获得了另一个智能解决方案,非常有用。@Max:谢谢!很高兴这有帮助!(我没有看到任何“一行”的答案,FWIW。)同样的问题在这里你的编辑明显改变了问题
wrapAll
仍然是如何包装的,这取决于如何选择要包装的内容。我更新了两个方法示例,具体取决于块的组织方式。这会让你找到正确的方向,即使你的具体结构恰好不适合这两个例子。
$(".first, .second").wrapAll('<div class="container"></div>');
$(".block").each(function() {
  $(this).find(".first, .second").wrapAll('<div class="container"></div>');
});
var current = $();

$(".first, .second").each(function() {
  var $this = $(this);
  if ($this.hasClass('first')) {
    doTheWrap(current);
    current = $();
  }
  current = current.add(this);
});
doTheWrap(current);

function doTheWrap(d) {
  d.wrapAll('<div class="container"></div>');
}
var current;

$(".first, .second").each(function() {
  var $this = $(this);
  if ($this.hasClass('first')) {
    current = $('<div class="container"></div>').insertBefore(this);
  }
  current.append(this);
});