gpt4 book ai didi

javascript - 动态 html 属性的字符串插值

转载 作者:行者123 更新时间:2023-12-05 07:45:30 25 4
gpt4 key购买 nike

下面是否可以使用字符串插值(注意动态属性)

document.body.innerHTML += <form id="digSigForm" action="${myObj.Url}" method="post"><input type="hidden" name="data" value="${myObj.someVal}"></form>

最佳答案

您应该使用反引号来定义带有字符串插值的字符串:``

像这样:

console.log(`1 and 1 make ${1 + 1}`);

这是来自 typescript documentation :

Another common use case is when you want to generate some string out of some static strings + some variables. For this you would need some templating logic and this is where template strings get their name from. Here's how you would potentially generate an html string previously:

var lyrics = 'Never gonna give you up';
var html = '<div>' + lyrics + '</div>';

Now with template strings you can just do:

var lyrics = 'Never gonna give you up';
var html = `<div>${lyrics}</div>`;

Note that any placeholder inside the interpolation (${ and }) is treated as a JavaScript expression and evaluated as such e.g. you can do fancy math.

console.log(`1 and 1 make ${1 + 1}`);

关于javascript - 动态 html 属性的字符串插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41564413/

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