在文本中捕捉换行符

时间:2012-08-27 14:43:09

标签: javascript

所以我在javascript中分析了一堆HTML,这样我就可以将这篇文章修改为30个字符,并附加类似“...阅读更多”等内容。问题是文本块是间隔的使用BR标记。这是我的代码;

<head runat="server">
<title></title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    window.onload = function onLoad() {
        alert("Start");
        var intros = this.document.getElementById('EventContent');
        if (intros) {
            for (i = 0; i < intros.length; i++) {

                var els = intros[i].childNodes;
                if (els) {
                    for (j = 0; j < els.length; j++) {
                        alert(els[j].tagName)
                    }
                }
            }
        }
        alert("Start");
    }

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="EventContent">
<br />


<br />
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<br />
</div>
</form>
</body>

但我的警告标记名永远不会显示,但我收到了警告STARTEND。我究竟做错了什么?我怎么能抓住那些愚蠢的lineBreaks ......

2 个答案:

答案 0 :(得分:1)

document.getElementById只返回一个元素;不要试图循环它。

window.onload = function() {
    alert("Start");

    var intro = document.getElementById('EventContent');
    var els = intro.childNodes;

    for (var i = 0; i < els.length; i++) {
        alert(els[i].tagName);
    }

    alert("Stop");
};

至于你只检索文本的最终目标,试试这个:

function getText(element) {
    var i, c, r = '';

    for(i = 0; c = element.childNodes[i]; i++) {
        if(c.nodeType === 3) {
            r += c.nodeValue;
        }
    }

    return r.replace(/^\s+|\s+$/g, '');
}

答案 1 :(得分:1)

您的变量'intros'不是数组,因此intros.length未定义。 您只需要遍历intros.childNodes

<head runat="server">
<title></title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
    window.onload = function onLoad() {
        alert("Start");
        var intros = this.document.getElementById('EventContent');
        if (intros) {
            for (i = 0; i < intros.childNodes.length; i++) {
                alert(intros.childNodes[i].tagName)
            }
        }
        alert("Start");
    }

</script>
</head>
<body>
<form id="form1" runat="server">
<div id="EventContent">
<br />
<br />
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<br />
</div>
</form>
</body>

那就是说,如果你还在使用jquery,你可能想尝试一下this way.

说,我可能只是获取div的内容(div.innerHTML),而不是迭代子节点,regex the <br />'s并用换行符替换,然后使用substring从其余字符中拆分前30个字符。

哦,如上所述,使用调试器。