2016-10-07 41 views
2

我試圖從這裏http://www.w3schools.com/howto/howto_js_tabs.asp用Node.js和受哈巴狗

這裏顯示使用多個代碼標籤混合JavaScript是我的代碼:

index.pug:

html 
    head 
    <script> 

function openCity(evt, cityName) { 
    // Declare all variables 
    var i, tabcontent, tablinks; 

    // Get all elements with class="tabcontent" and hide them 
    tabcontent = document.getElementsByClassName("tabcontent"); 
    for (i = 0; i < tabcontent.length; i++) { 
     tabcontent[i].style.display = "none"; 
    } 

    // Get all elements with class="tablinks" and remove the class "active" 
    tablinks = document.getElementsByClassName("tablinks"); 
    for (i = 0; i < tablinks.length; i++) { 
     tablinks[i].className = tablinks[i].className.replace(" active", ""); 
    } 

    // Show the current tab, and add an "active" class to the link that opened the tab 
    document.getElementById(cityName).style.display = "block"; 
    evt.currentTarget.className += " active"; 
} 
</script> 

    title= title 
    body 
    h3= message 

<link rel="stylesheet" type="text/css" href="style.css"> 

<ul class="tab"> 
    <li><a href="#" class="tablinks" onclick="openCity(event, 'London')">London</a></li> 
    <li><a href="#" class="tablinks" onclick="openCity(event, 'Paris')">Paris</a></li> 
    <li><a href="#" class="tablinks" onclick="openCity(event, 'Tokyo')">Tokyo</a></li> 
</ul> 

<div id="London" class="tabcontent"> 
    <h3>London</h3> 
    <p>London is the capital city of England.</p> 
</div> 

<div id="Paris" class="tabcontent"> 
    <h3>Paris</h3> 
    <p>Paris is the capital of France.</p> 
</div> 

<div id="Tokyo" class="tabcontent"> 
    <h3>Tokyo</h3> 
    <p>Tokyo is the capital of Japan.</p> 
</div> 

風格。 css:

/* Style the list */ 
ul.tab { 
    list-style-type: none; 
    margin: 0; 
    padding: 0; 
    overflow: hidden; 
    border: 1px solid #ccc; 
    background-color: #f1f1f1; 
} 

/* Float the list items side by side */ 
ul.tab li {float: left;} 

/* Style the links inside the list items */ 
ul.tab li a { 
    display: inline-block; 
    color: black; 
    text-align: center; 
    padding: 14px 16px; 
    text-decoration: none; 
    transition: 0.3s; 
    font-size: 17px; 
} 

/* Change background color of links on hover */ 
ul.tab li a:hover {background-color: #ddd;} 

/* Create an active/current tablink class */ 
ul.tab li a:focus, .active {background-color: #ccc;} 

/* Style the tab content */ 
.tabcontent { 
    display: none; 
    padding: 6px 12px; 
    border: 1px solid #ccc; 
    border-top: none; 
} 

我收到此錯誤:

9|  // Get all elements with class="tabcontent" and hide them 
    10|  tabcontent = document.getElementsByClassName("tabcontent"); 
> 11|  for (i = 0; i < tabcontent.length; i++) { 
---------------^ 
    12|   tabcontent[i].style.display = "none"; 
    13|  } 
    14| 

malformed each 
    at makeError (/Users/node_modules/pug/node_modules/pug-lexer/node_modules/pug-error/index.js:32:13) 
    at Lexer.error (/Users/node_modules/pug/node_modules/pug-lexer/index.js:58:15) 
    at Lexer.each (/Users/node_modules/pug/node_modules/pug-lexer/index.js:911:12) 
    at Lexer.callLexerFunction (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1315:23) 
    at Lexer.advance (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1343:15) 
    at Lexer.callLexerFunction (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1315:23) 
    at Lexer.getTokens (/Users/node_modules/pug/node_modules/pug-lexer/index.js:1371:12) 
    at lex (/Users/node_modules/pug/node_modules/pug-lexer/index.js:12:42) 
    at Object.load.string.lex (/Users/node_modules/pug/lib/index.js:93:27) 
    at Function.loadString [as string] (/Users/node_modules/pug/node_modules/pug-load/index.js:44:24) 

如何將javascript與帕格混合?

回答

1

沒有與哈巴狗NPM頁面(https://www.npmjs.com/package/pug)上內嵌的JavaScript的一個小例子。基本上它是這樣的:

html 
    head 
     script (type="text/javascript"). 
      /* your javascript here */ 
     title= title 

另外我不確定你爲什麼使用部分帕格語法和部分HTML。例如,你有這樣的:

<link rel="stylesheet" type="text/css" href="style.css"> 

當它應該是這樣的:

link(rel="stylesheet" type="text/css" href="style.css") 

作爲替代(對於JavaScript的),您可以使用include指令,如下解釋:https://pugjs.org/language/includes.html,或者你可以把JavaScript放在一個外部文件中(就像你用css做的那樣),只需要這麼做:

script(src='app.js') 
+0

我的最終評論(注意這是一條評論,而不是答案的一部分)是,如果你真的想要標籤,你可以考慮使用像twitter bootstrap之類的東西,並免費獲得它,而無需編寫任何CSS或JavaScript,請看這裏例如:http://getbootstrap.com/javascript/#tabs – Kevin

0

首先要嘗試(whitout更多細節)

如果你使用嚴格的模式改變這一點:

for (i = 0; i < tabcontent.length; i++) 

這個

for (var i = 0; i < tabcontent.length; i++) 

如果它的工作原理是教你課程:宣告一切,無處不在。總是

如果沒有,我們需要更多的細節

+0

你正在糾正他的javascript,但問題與他對帕格的使用有關。儘管我不會投票,但你的回答與他所要求的無關。 – Kevin