计算文本块中的字符数,并将其拆分为两列
Count characters within block of text and split into two columns
我正在尝试将一块文本自动转换为列,因为客户端没有很好的html/CSS。
我写了这个小脚本来搜索div以找到hr标记,并自动向p标记添加一个类。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
if($('#data').find('hr')){
$('hr').prev('p').addClass('left');
$('hr').next('p').addClass('right');
}
});
</script>
</head>
<body>
<div id="data">
<p>This is paragraph 1. Lorem ipsum ... <br /><br />
This is paragraph 2. Lorem ipsum ... <br /><br />
This is paragraph 3. Lorem ipsum ... </p>
<hr />
<p>This is paragraph 4. Lorem ipsum ... <br /><br />
This is paragraph 5. Lorem ipsum ... <br /><br />
This is paragraph 6. Lorem ipsum ... </p>
</div>
但是,如果段落的长度不同,这就不太好了,因为右栏可能比左栏大。
所以我需要重写它来计算字符数,并将其平均分割(或者与左边较大的部分一样接近),但我不知道如何开始。所以如果你能帮上忙,那就太棒了。
首先不应该通过JavaScript/jQuery对客户端进行渲染来实现这一点——你不应该构建布局,尽管JavaScript/jQuery只是改进了它,
但是对于您的代码,您可以这样做。
http://jsfiddle.net/barkermn01/VTQeC/10/
然后,如果您希望splitChar在sentances上中断,则可以将其更改为"."
;如果您喜欢单词breakdown ,则可以保留在" "
上
考虑到正文中有段落标记,这应该可以在没有太多修改的情况下工作。无论你有多少段,无论文本内容如何在它们之间传播,它都应该有效。
/**
* Create left and right divs
*/
var $left = $(document.createElement("div")).addClass('left');
var $right = $(document.createElement("div")).addClass('right');
/**
* Store the text lengths into an array
*/
var textLengthArray = [];
var totalLength = 0;
$('p').each(function(){
var $this = $(this);
var text = $this.text();
textLengthArray[textLengthArray.length] = text.length;
totalLength += text.length;
});
/**
* The midpoint is crucial
*/
var midpoint = totalLength / 2;
/**
* pastParagraphs - is the text length of the paragraphs we've already moved
* currentParagraphPos - keeps track of where we are
* split - we only want to split one paragraph in two
*/
var pastParagraphs = 0
var currentParagraphPos = 0;
var split = false;
/**
* iterate over the textLengthArray (i.e. the paragraphs)
* add them to the left until the text length would of all the paragraphs we've moved
* into the left would exceed the midpoint. Then split the paragraph the midpoint falls into
* from there on move paragraphs to the right div.
*/
for (i in textLengthArray) {
/**
* This is the check to insure we aren't past the midpoint
*/
if (pastParagraphs + textLengthArray[currentParagraphPos] < midpoint) {
/**
* Move the first paragraph to the left div.
* We always move the first because they filter down as we remove them.
*/
$left.append($('p').eq(0));
pastParagraphs += textLengthArray[currentParagraphPos];
currentParagraphPos++;
}
else {
/**
* if we haven't split a paragraph already we should split this first paragraph going right in two.
*/
if (!split) {
split = true;
/**
* Create two new paragraphs, one for the left, one for the right
*/
var $goLeft = $(document.createElement("p"));
var $goRight = $(document.createElement("p"));
/**
* The split shouldn't fall in the middle of a word
* so we run a while loop to find the next available space.
*/
var splitIndex = midpoint - pastParagraphs;
var splitPHtml = $('p').eq(0).html();
while (splitPHtml.charAt(splitIndex) != ' ') {
splitIndex++;
}
/**
* As we've created two new paragraphs we need to manually remove this one
*/
$('p').eq(0).remove();
/**
* Add the ontent to our new paragraphs and append them to the left and right divs
*/
$goLeft.html(splitPHtml.substring(0, splitIndex));
$goRight.html(splitPHtml.substring(splitIndex, splitPHtml.length));
$left.append($goLeft);
$right.append($goRight);
}
else {
/**
* Add any further paragraphs to the right div
*/
$right.append($('p').eq(0));
currentParagraphPos++;
}
}
}
/**
* Finally we want to append our divs to the body.
*/
$('body').append($left);
$('body').append($right);
相关文章:
- 带组的两列html表
- 单个交叉筛选器维度和两列上的组
- 使用 php 将类别和子类别显示为两列
- 将网格视图的两列相乘,并将其显示在新列中
- Angular ng sortable-如果我在一个栏中添加了一个搜索框,则拖放在两列之间无法正常工作
- 将列表拆分为两列.列表没有't具有块,但是需要在最接近的<p>标签
- 在两列中动态显示产品
- 两列全高布局展开以包含内容
- 计算文本块中的字符数,并将其拆分为两列
- 如何使用AngularJS而不是在查询子句中根据两列的差异对表进行排序
- 附加到两列中的一列,然后附加到另一列
- 两列布局,两列高度相同且均填充屏幕的整个高度
- 具有多个不同高度小部件的两列布局,如何使小部件可折叠并保持页面的流动性
- 在两列中浮动不同数量的元素(左和右),即使高度不同
- 使用ISCroll实现两列无限滚动
- dataTables (jquery) 按两列搜索
- jqgrid:基于两列排序
- 并排显示两列 JavaScript
- 使用两列表示日期和时间与 Dygraph
- 比较两列中的随机数字集