2015-04-03 142 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