博客中的'data:post-snippet'截断

问题描述 投票:-1回答:2

假设我在Blogger上写了这样的帖子:abc def ghi <div class =“pls-exclude-from-snippet”> jkl mno pqr stu vwxyz </ div>

我希望Blogger只将其数据设置为'abc def ghi'(让Blogger知道如果它读取令牌就停止了:一个带有类'pls-exclude-from-snippet的div,而不是从a到z列出“)。我怎么做?

html post blogger code-snippets truncate
2个回答
0
投票

我在这里找到了帮助https://productforums.google.com/forum/#!topic/blogger/X9LxrjXnb2s

仍然需要调整一下,但它的工作原理。想要发布它来帮助那些遇到类似问题的人。

这是我的代码。在</head>之前,添加:

<script type='text/javascript'> //<![CDATA[ function stopIfFound(strx,token){ var theLocation = strx.indexOf(token); if(theLocation!=-1){ strx = strx.substr(0,theLocation); } return strx; } function removeHtmlTag(strx, chop){ strx = stopIfFound(strx,'<div class="pls-exclude-from-snippet">'); if(strx.indexOf("<")!=-1){ var snippet = strx.split("<"); for(var i=0;i<snippet.length;i++){ if(snippet[i].indexOf(">")!=-1){ snippet[i] = snippet[i].substring(snippet[i].indexOf(">")+1,snippet[i].length); } } strx = snippet.join(""); } chop = (chop < strx.length-1) ? chop : strx.length-2; while(strx.charAt(chop-1)!=' ' && strx.indexOf(' ',chop)!=-1) chop++; strx = strx.substring(0,chop-1); return strx+'...'; } function createSnippet(pID){ var div = document.getElementById(pID); var summ = snippet_count; var summary = '<div class="snippets">' + removeHtmlTag(div.innerHTML,summ) + '</div>'; div.innerHTML = summary; } //]]> </script>

找到<data:post.snippet/>,in

 <div class='post-body'>
        <b:if cond='data:post.snippet'>
           <data:post.snippet/>
        </b:if>
      </div>

用它替换它

<div expr:id='&quot;summary&quot; + data:post.id'><data:post.body/></div>
<script type='text/javascript'>createSnippet(&quot;summary<data:post.id/>&quot;);  
  </script>

0
投票

您可以尝试限制字符的长度。

使用以下语法:

<b:eval expr='snippet(data:post.body, {length: 150})' />

150是长度。您可以将其更改为您想要的任何数字。

查看我的个人博客change snippet length on Blogger without Javascript以查看更多示例。

© www.soinside.com 2019 - 2024. All rights reserved.