javascript - Javascript模板文字缺少变量值
问题描述
我有一个简单的模板格式化程序,用户填写某些信息,我的程序输出一个定制的字母。
const name = document.getElementById('name')
const food = document.getElementById('food')
const age = document.getElementById('age')
const submitBtton = document.getElementById('submit')
const letter = document.getElementById('letter')
function output(){
letter.textContent =
`Happy Birthday ${name.value}! You're turning ${age.value}
This is a big accomplishment, we must celebrate. Are you free next week for some ${food.value}?`
}
submitBtton.addEventListener('click',output);
<form>
<input type = "text" id = "name" placeholder = "name">
<input type = 'text' id = 'food' placeholder = 'food'>
<input type = 'text' id = 'age' placeholder = 'age'>
<button id = 'submit' type = 'button' >Submit</button>
<button id = 'resetting' type = 'reset'>Reset</button>
</form>
<p id = "letter"></p>
上面的工作很好,但是如果其他一切都保持不变,但我将 letter.textContent 分配给另一个包含模板文字的变量:
<script>
const paragraph =
`Happy Birthday ${name.value}! You're turning ${age.value}
This is a big accomplishment, we must celebrate. Are you free next week for some ${food.value}?``
function output() {letter.textContent = paragraph}
submitBtton.addEventListener('click',output)
</script>
该程序仅在大约 30% 的时间内有效。字符串文字输出正常,但变量 - 名称、年龄和食物并不总是出现?这是为什么?
解决方案
该脚本在加载后获取 ,value
和属性。name
age
food
<script>
/**
* This part will be executred once when the script is loaded
* At this phase your "name.value", "age.value", and "food.value" are empty
* */
const paragraph =
`Happy Birthday ${name.value}! You're turning ${age.value}
This is a big accomplishment, we must celebrate. Are you free next week for
some ${food.value}?`
// This function will be called everytime, and it will return the same result
// because "paragraph" variable didn't change
function output() {
letter.textContent = paragraph
}
submitBtton.addEventListener('click', output)
</script>
在 Javascript 中,字符串按值传递:因此,例如,当您调用时name.value
,您只是复制了该值,它是一个空字符串""
。
因此,如果您想在函数paragraph
之外拥有并“更新值” output
,只需将其设为函数,以便每次都会调用它,然后它将获取新值。
例子:
<form>
<input type="text" id="name" placeholder="name">
<input type='text' id='food' placeholder='food'>
<input type='text' id='age' placeholder='age'>
<button id='submit' type='button'>Submit</button>
<button id='resetting' type='reset'>Reset</button>
</form>
<p id="letter"></p>
<script>
const name = document.getElementById('name')
const food = document.getElementById('food')
const age = document.getElementById('age')
const submitBtton = document.getElementById('submit')
const letter = document.getElementById('letter')
function getParagraph() {
return `Happy Birthday ${name.value}! You're turning ${age.value}
This is a big accomplishment, we must celebrate. Are you free next week for
some ${food.value}?`
}
function output() {
letter.textContent = getParagraph()
}
submitBtton.addEventListener('click', output)
</script>
推荐阅读
- python - 确定使用 Kubernetes 部署的 Dask 调度程序 IP 地址
- typescript - 带有外部 api 的单元测试服务
- browser - Neo4J中如何访问浏览器界面安装APOC?
- content-management-system - 基于角色的受限内容,带有没有内置本地成员的无头 CMS
- swiftui - ScrollView 内容出现在 ScrollView 框架之外
- javascript - Extjs复选框框标签颜色不变
- sql - SQL - 按最新更新对数据进行分组
- docker - Pion 自定义 SFU 服务器无法在 docker 内工作
- html - React 和垂直响应式设计
- node.js - Mailgun 在 Node.js 应用程序中看不到凭据