JQuery收集一组元素,将每个元素展开,然后将所有元素包裹在新元素中
JQuery collect group of elements, unwrap each and then wrap all in new element
我正在尝试转换标记,其中每行都是<p>
的企业列表,只有企业名称也包装在<strong>
中。相反,我希望每个企业的整个清单都包装在一个<p>
中,并使用<br/>
来分隔行。下面是原始标记:
<div class="wrapper">
<p><strong>Name of Business 1</strong></p>
<p>Membership Level 1</p>
<p>Name of Owner 1</p>
<p>555 Someplace Ave 1</p>
<p>Nowhere, OH 55555 1</p>
<p>555-555-5555 1</p>
<p><a href="mailto:test@test.com">test@test.com</a> 1</p>
<p>Business Description 1</p>
<p><strong>Name of Business 2</strong></p>
<p>Membership Level 2</p>
<p>Name of Owner 2</p>
<p>555 Someplace Ave 2</p>
<p>Nowhere, OH 55555 2</p>
<p>555-555-5555 2</p>
<p><a href="mailto:test@test.com">test@test.com</a> 2</p>
<p>Business Description 2</p>
</div>
下面是我需要修改的内容:
<div class="wrapper">
<p>
<strong>Name of Business 1</strong><br>
Membership Level 1<br>
Name of Owner 1<br>
555 Someplace Ave 1<br>
Nowhere, OH 43021 1<br>
555-555-5555 1<br>
<a href="mailto:test@test.com">test@test.com</a> 1<br>
Business Description 1<br>
</p>
<p>
<strong>Name of Business 2</strong><br>
Membership Level 2<br>
Name of Owner 2<br>
555 Someplace Ave 2<br>
Nowhere, OH 43021 2<br>
555-555-5555 2<br>
<a href="mailto:test@test.com">test@test.com</a> 2<br>
Business Description 2<br>
</p>
</div>
我设法用一些最丑陋、最复杂的代码完成了它。基本上,我采取了一步一步的方法,因为我无法得到任何链式解决方案。
这是一个不幸的解决方案:
jQuery('p > strong').parent('p').addClass('strong').has('br').addClass('clean');
jQuery('p:not(.clean)').addClass('unwrap');
jQuery('.unwrap.strong').nextUntil('.strong').each( function(){
var theHTML = jQuery(this).html();
jQuery(this).replaceWith('<span class="tomove">' + theHTML + '<br/></span>');
});
jQuery('span.tomove').each(function(){
jQuery(this).appendTo( jQuery(this).prev('p.strong') );
});
jQuery('.unwrap strong').each(function(){
var theHTML = jQuery(this).html();
jQuery(this).replaceWith('<strong>' + theHTML + '</strong><br/>');
});
jQuery('span.tomove').replaceWith(function(){
return jQuery(this).contents();
});
jQuery('p.strong.unwrap').addClass('clean').removeClass('unwrap');
p {background:yellow}
strong {background:red;color:white;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="wrapper">
<p><strong>Name of Business 1</strong></p>
<p>Membership Level 1</p>
<p>Name of Owner 1</p>
<p>555 Someplace Ave 1</p>
<p>Nowhere, OH 55555 1</p>
<p>555-555-5555 1</p>
<p><a href="mailto:test@test.com">test@test.com</a> 1</p>
<p>Business Description 1</p>
<p><strong>Name of Business 2</strong></p>
<p>Membership Level 2</p>
<p>Name of Owner 2</p>
<p>555 Someplace Ave 2</p>
<p>Nowhere, OH 55555 2</p>
<p>555-555-5555 2</p>
<p><a href="mailto:test@test.com">test@test.com</a> 2</p>
<p>Business Description 2</p>
</div>
我想有一种方法可以在一个链式命令中处理这个问题,但是我一直在尝试使用.nextUntil()
和/或.add()
构建引用,同时试图打开每个<p>
并在包装每个组之前在该链中添加<br/>
。
有谁能提供更好的解决方案吗?
我是这么想的;无论业务的数量和数据参数的长度如何,它都应该工作。它确实要求第一个"p"元素包含一个"强"元素,并且除了企业名称之外,其他数据字段都没有强元素:
$(document).ready(function(){
var businesses = [], bizIndex = -1;
$('.wrapper > p').each(function(i){
var html = $(this).html();
if ($(this).find('strong').length) {
bizIndex++;
businesses.push([html]);
}
else {
businesses[bizIndex].push(html);
}
});
$('.wrapper').empty();
$.each(businesses, function(i) {
var business = businesses[i];
$('.wrapper').append($('<p>').html(business.join('<br>')+'<br>'));
});
});
https://jsfiddle.net/2rz7jp4z/3/下面使用Strong元素作为分隔符。提供的JSbin将它们添加到一个新的div中,以显示before/after。
http://jsbin.com/soyelozemi/4/editjQuery(document).ready(function(){
var insert = $('<div class="wrapper2"></div>');
var tempHolder = $('<p></p>');
jQuery('.wrapper p').each(function(index){
jQuery(tempHolder).append($(this).html() + '<br/>');
if(jQuery(this).next().find('strong').length || index == jQuery('.wrapper p').length-1){
jQuery(insert).append(tempHolder);
tempHolder = jQuery('<p></p>');
}
});
jQuery('.after').append(jQuery(insert));
});
相关文章:
- 将事件侦听器添加到文档,而不是签入元素存在,然后添加事件侦听器
- 将元素移动到右侧,然后返回到左侧
- 添加animate.css类,然后使用remove移除元素
- Angular js,在选择元素上,我想 POST 数据以将其保存在数据库中,然后我想使用 PUT 更新它而无需重新加载
- 在元素中搜索字符串,然后将其放入条件语句中
- 从异步调用返回数组,然后为数组的每个元素返回其他异步调用
- JQuery 测试元素是可见的还是隐藏的,然后添加删除类
- 为什么这个脚本只工作一次?只替换元素一次,然后再也不替换
- 如何删除元素的结束标记,添加一些文本,然后重新添加标记
- jQuery检查URL,然后显示和隐藏元素
- Javascript(动态)插入到数组中,然后移动+1下面的所有元素
- jQuery-等待此元素,然后执行1次
- 如何正确地等待元素获得正确的宽度,然后在Angular 2中访问它
- ()插入元素,然后将其取回
- 从数组中删除元素,然后保留其状态以备将来使用,而不使用全局变量
- jQuery查找元素,然后组成逗号分隔的列表
- 使用Raphael.js可以相对定位一个元素,然后将其偏移一个绝对单位
- 我如何让我的网站检测鼠标是否被使用,然后在元素中添加一个类
- 关于在动态创建的元素中添加预先样式化的类或在那里添加stying&然后
- 按类和自定义属性值查找元素(然后更新它)