2015-04-03 31 views
2

我有一个网页显示一些其中有一些代码的帖子。我正在尝试使用突出显示的js来突出显示代码。但是,我仍然无法在几个小时后开始工作。这是我第一次使用Meteor,因此代码从他们网站上的教程改编而来。整个项目很简单,这里是我的主要js文件:不能让高亮js在流星js工作

Posts = new Mongo.Collection("posts"); 

if (Meteor.isClient) { 
    // This code only runs on the client 
    Template.body.helpers({ 
     posts: function() { 
     return Posts.find({}, {limit: 15}); 
     } 
    }); 

    Template.post.rendered = function(){ 
     $('pre code').each(function(i, block) { 
     hljs.highlightBlock(block); 
     }); 
    }; 
} 

我使用的HTML文件:

<head> 
    <title>Posts</title> 
</head> 

<body> 
    <div class="container"> 
    <header> 
     <h1>Posts</h1> 
    </header> 

    <ul> 
     {{#each posts}} 
     {{> post}} 
     {{/each}} 
    </ul> 
    </div> 
</body> 

<template name="post"> 
    <h1 class="text">{{title}}</h1> 
    {{{ content }}} 
</template> 

我也有从大亮点JS' github上斧头代码风格的css文件项目中的回购。加载页面后,hljs作为类值添加到代码标记中,但代码标记内的代码保持不变,如下图所示。 code unchanged

任何想法为什么突出js没有改变代码?

如果我的描述不够清楚,我会添加更多信息。

+1

将有助于有一个[meteorpad](http://meteorpad.com/)演示。 – 2015-04-03 05:34:10

+1

@FlorianF。嗨,我去了那里,做了一个演示,它在那里工作。所以我重新构建了我的项目,就像演示项目一样,它也工作得很好。感谢您的建议! – Gnijuohz 2015-04-03 18:43:13

回答

0

问题已解决。我试图按照@Florian的建议在meteorpad上进行演示,并在那里以某种方式工作。
所以我回到我的项目,并重新构造它像meteorpad上的演示项目,它以某种方式工作。我结束了使用simple:highlighthere

1

@Gnijuohz

我有使用highlight封装的演示。

这里是Source CodeDEMO

这是我如何使用它。

流星版本< 1.1

Template.example.rendered = function(){ 

    /* 
    Higligth Configuration using 
    https://highlightjs.org 
    */ 
    hljs.configure({ 
     tabReplace: ' ', 
     classPrefix: '', 
     useBR:true 

    }) 
    $('pre code').each(function(i, block) { 
    hljs.highlightBlock(block); 
    }); 

    } 

在新版本流星(1.1)。

Template.example.onRendered(function(){ 
/* 
     Higligth Configuration using 
     https://highlightjs.org 
     */ 
     hljs.configure({ 
      tabReplace: ' ', 
      classPrefix: '', 
      useBR:true 

     }) 
     $('pre code').each(function(i, block) { 
     hljs.highlightBlock(block); 
     }); 
}) 
+0

谢谢!我真的解决了它,并已经回答了这个问题。 – Gnijuohz 2015-04-04 17:20:55

+0

我无法在控制台中没有“Uncaught ReferenceError:hljs is defined”错误的情况下运行highlighter.js。我来到这里,所有答案的所有链接都被打破了。上面的脚本不起作用。 – Deborah 2016-06-07 21:30:20