无法剪切太长的标题

时间:2015-07-17 04:43:11

标签: javascript html

我有一段javascript,应检查我网站上所有标题的长度,如果它们太短,则剪切它们,但这并不会发生。为什么呢?

var titles = document.getElementsByClassName("newsTitle");
for (i=0; i < titles.length; i++) {
    if (titles[i].length > 14) {
        var oldTitle = titles[i].value;
        var newTitle = oldTitle.substring(0,10) + "...";
        titles[i].innerHTML = newTitle;
    }
}

身体里的一切

<div id="header">
    <h1 id="headerTitle">WIP</h1>
    <ul id="headerList">
        <li class="headerListButton"><a href="http://mastermeredith.com" class="headerListText">Home</a></li>
        <li class="headerListButton"><a href="about.html" class="headerListText">About</a></li>
        <li class="headerListButton"><a href="archives.html" class="headerListText">Archives</a></li>
    </ul>
</div>
<div id="newsHolder">
    <!--News pieces go here-->
    <?php
        $newsPieces = scandir("posts", 1);
        for ($i = 0; $i < count($newsPieces) and $i < 12; $i++) {
            if ($newsPieces[$i] != "." and $newsPieces[$i] != "..") {
                $titlefile = fopen("posts/" . $newsPieces[$i] . "/title.txt", "r");
                $textfile = fopen("posts/" . $newsPieces[$i] . "/text.txt", "r")
                $fullElement = "<div class='newsPiece'><div class='newsImageHolder'><img class='newsImage' src='posts/" . $newsPieces[$i] . "/image.jpg'></div><div class='newsContent'><h1 class='newsTitle'>" . fread($titlefile, filesize("posts/" . $newsPieces[$i] . "/title.txt")) . "</h1><p class='newsText'>" . fread($textfile, filesize("posts/" . $newsPieces[$i] . "/text.txt")) . "</p><a class='newsLink' href='post?post=" . urlencode($newsPieces[$i]) . "'>Continue Reading</a></div></div>";
                echo $fullElement;
                fclose($titlefile);
                fclose($textfile);
            }
        }
    ?>
    <script type="text/javascript">
        var titles = document.getElementsByClassName("newsTitle");
        for (i=0; i < titles.length; i++) {
            if (titles[i].length > 14) {
                var oldTitle = titles[i].value;
                var newTitle = oldTitle.substring(0,10) + "...";
                titles[i].innerHTML = newTitle;
            }
        }
    </script>
</div>

2 个答案:

答案 0 :(得分:2)

您需要使用.innerHTML代替titles[i].value;,

var oldTitle = titles[i].innerHTML;

因为我理解titles不是输入,所以你还需要检查标题长度

&#13;
&#13;
var titles = document.getElementsByClassName("newsTitle"),
    title;

for (var i = 0, len = titles.length; i < len; i++) {
    title = titles[i].innerHTML;
    
    if (title.length > 14) {
        titles[i].innerHTML = title.substring(0,10) + "...";
    }
}
&#13;
<h1 class="newsTitle">Test title</h1>
<h1 class="newsTitle">Test title Test title Test title</h1>
<h1 class="newsTitle">Test title Test title Test title</h1>
<h1 class="newsTitle">Test title Test title Test title Test title</h1>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您需要测试titles[i].innerHTML

的长度
if (titles[i].innerHTML.length > 14) {
    var oldTitle = titles[i].innerHTML;
    var newTitle = oldTitle.substring(0, 10) + "...";
    titles[i].innerHTML = newTitle;
}