计算文本块中的字符数,并将其拆分为两列

Count characters within block of text and split into two columns

本文关键字:拆分 两列 文本 字符 计算      更新时间:2024-02-09

我正在尝试将一块文本自动转换为列,因为客户端没有很好的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);