假设我在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列出“)。我怎么做?
我在这里找到了帮助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='"summary" + data:post.id'><data:post.body/></div>
<script type='text/javascript'>createSnippet("summary<data:post.id/>");
</script>
您可以尝试限制字符的长度。
使用以下语法:
<b:eval expr='snippet(data:post.body, {length: 150})' />
150是长度。您可以将其更改为您想要的任何数字。
查看我的个人博客change snippet length on Blogger without Javascript以查看更多示例。