javascript - Pug 文件无法识别 jQuery
问题描述
我的 Node/Express 应用程序有一个简单的 Pug 界面,其中包括一个 WebChat 小部件和一些包含 iFrame 的选项卡。我想使用 jQuery 片段来捕获变量并将其放入位于选项卡 iFrame 内的 URL 中。现在,我只是想让 Wikipedia URL 工作(变量是inputMessage)。
问题是我需要 jQuery 成为我的 WebChat 小部件脚本的一部分,以便从其输入框中捕获用户的输入(到变量中),但是脚本运行并且页脚选项卡甚至还没有创建,所以当项目运行时,我从维基百科得到一个“未定义”作为 URL,因为inputMessage
从未正确放置在那里。但我也不能在脚本之前放置页脚,因为选项卡式 iFrame 在页面上需要低于聊天小部件。我想有很多方法可以实现这一目标。我是 Pug 和脚本的新手,所以我还不知道有什么方法。
指数.pug
extends layout
block content
doctype html
html(lang="en")
head
meta(charset='utf-8')
meta(name='viewport' content='width=device-width, initial-scale=1')
script(src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js')
article
ul
li.title Study Bot
li.robot
img(src='/images/robot-face.jpg' alt='Robot face')
chat-window
#webchat(action='/chat', method='POST')
script(src='https://cdn.botframework.com/botframework-webchat/latest/webchat.js')
script.
const styleSet = window.WebChat.createStyleSet({
bubbleBackground: 'rgba(252, 229, 53, 1)',
bubbleFromUserBackground: 'rgba(4, 234, 104, 1)',
paddingRegular: 10,
sendBoxHeight: 50,
bubbleMinWidth: 400,
bubbleMaxWidth: 700
});
window.WebChat.renderWebChat({
directLine: window.WebChat.createDirectLine({ secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' }),
styleSet
}, document.getElementById('webchat'));
// Add on keypress listener to text input and wait for the user to hit the `enter` key
$("input[aria-label='Type your message']").on('keypress', event => {
// Check if user pressed `enter`
if (event.which === 13){
var inputMessage = $("input[aria-label='Type your message']").val();
document.getElementById('#tab-window').innerHTML = inputMessage;
}
});
footer
.tab
button.tablinks(onclick="openSite(event, 'Encyclopedia')") Encyclopedia
button.tablinks(onclick="openSite(event, 'MSAcademic')") Microsoft Academic
button.tablinks(onclick="openSite(event, 'NewsBlogs')") News / Blogs
#Encyclopedia.tabcontent
iframe#tab-window(src=`https://en.wikipedia.org/wiki/${inputMessage}`)
#MSAcademic.tabcontent
iframe#tab-window(src='https://academic.microsoft.com/')
#NewsBlogs.tabcontent
iframe#tab-window(src='https://www.bing.com/')
script
include ../routes/index.js
解决方案
原来我有一个:
doctype html
html(lang="en")
head
在我的 index.pug 文件和我的 layout.pug 文件中。我只在我的 layout.pug 文件中需要它,所以我猜这个脚本在阅读时遇到了麻烦。由于我是 Pug 的新手,所以我忽略了这一点。所以现在我有以下有效的代码:
指数.pug
extends layout
block content
article
ul
li.title Study Bot
li.robot
img(src='/images/robot-face.jpg' alt='Robot face')
chat-window
#webchat(action='/chat', method='POST')
script(src='https://cdn.botframework.com/botframework-webchat/latest/webchat.js')
script.
const styleSet = window.WebChat.createStyleSet({
bubbleBackground: 'rgba(252, 229, 53, 1)',
bubbleFromUserBackground: 'rgba(4, 234, 104, 1)',
paddingRegular: 10,
sendBoxHeight: 50,
bubbleMinWidth: 400,
bubbleMaxWidth: 700
});
window.WebChat.renderWebChat({ directLine: window.WebChat.createDirectLine({
secret: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' }),
styleSet},
document.getElementById('webchat'));
// Add on keypress listener to text input and wait for the user to hit the `enter` key
$("input[aria-label='Type your message']").on('keypress', event => {
// Check if user pressed enter
if (event.which === 13) {
var inputMessage = $("input[aria-label='Type your message']").val();
var wiki = 'https://en.wikipedia.org/wiki/' + inputMessage;
var encycl = 'https://academic.microsoft.com/#/search?iq=%40' + inputMessage + '%40&q=' + inputMessage;
var newsBlogs = 'https://www.bing.com/search?q=' + inputMessage;
document.getElementById('tab1-window').src = wiki;
document.getElementById('tab2-window').src = encycl;
document.getElementById('tab3-window').src = newsBlogs;
}
});
tabs-container
.tab
button.tablinks(onclick="openSite(event, 'Encyclopedia')") Encyclopedia
button.tablinks(onclick="openSite(event, 'MSAcademic')") Microsoft Academic
button.tablinks(onclick="openSite(event, 'NewsBlogs')") News / Blogs
#Encyclopedia.tabcontent
iframe#tab1-window(src='https://en.wikipedia.org/wiki/')
#MSAcademic.tabcontent
iframe#tab2-window(src='https://academic.microsoft.com/')
#NewsBlogs.tabcontent
iframe#tab3-window(src='https://www.bing.com/')
布局.pug
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/main.css')
meta(charset='utf-8')
meta(name='viewport' content='width=device-width, initial-scale=1')
script(src='https://code.jquery.com/jquery-3.3.1.min.js')
script
include ../routes/index.js
body
block content
推荐阅读
- javascript - 使用 CryptoJS 解密外部 AES256/CBC/PKCS7 数据,提供来自 SiteMinder 的 64 字符十六进制密钥
- java - Jxbrowser 6.20:在大尺寸 Pdf 中搜索(大约以 GB 为单位)抛出异常 com.teamdev.jxbrowser.chromium.internal.ipc.IPCException
- r - sparklyr- JAVA_HOME 已设置,但未指向有效版本。
- hstore - postgresql - 从嵌套的 hstore 中选择然后删除
- node.js - 多个文件移动到 NodeJS 中的另一个目录
- postgresql - 这是在 Postgres 中创建部分索引的正确方法吗?
- spring-boot - Spring Boot 和 Spring JDBC Session 的重复键异常
- ios - watchOS:如何在没有心率传感器的情况下使用 HKWorkoutSession
- java - 如何从填充了数据库信息的 HTML 选择标签中检索客户端选择?
- c++ - 使用 cmake 为 QNX 交叉编译 gRPC