gpt4 book ai didi

node.js - 将 javascript 与 node.js 和 pug 混合

转载 作者:搜寻专家 更新时间:2023-10-31 22:47:12 26 4
gpt4 key购买 nike

我正在尝试使用此处的代码显示多个选项卡 http://www.w3schools.com/howto/howto_js_tabs.asp

这是我的代码:

索引.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 与 pug 混合使用?

最佳答案

在 pug 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')

关于node.js - 将 javascript 与 node.js 和 pug 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39921473/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com