这可能听起来很傻但我在这段代码中打印变量时遇到问题 -
var clsuffix = "something"; $("#box").wrap('<div class="boxy" />');我想在class =“ here ”中打印clsuffix变量,所以它可能就像
<div class="boxysomething">但是当我在class =“”中使用变量时,它会按原样出现(不被视为变量)
This might sound silly but i'm having trouble printing a variable in this code -
var clsuffix = "something"; $("#box").wrap('<div class="boxy" />');I want to print the clsuffix variable inside the class=" here " so it could be like
<div class="boxysomething">But when i use the variable inside class="", it comes up as it is (not being treated as variable)
最满意答案
只需将其连接起来。最终,您的目标是构建一个由字面"boxy"和变量clsuffix的内容组成的clsuffix 。 这是用JavaScript中的"boxy" + clsuffix完成的:
$("#box").wrap('<div class="boxy' + clsuffix + '" />');如果您只是尝试将变量插入到字符串中(例如您可能在PHP双引号字符串中执行),则JavaScript不会解释它:
// Doesn't work in JavaScript: $("#box").wrap('<div class="boxyclsuffix" />');Just concatenate it in. Ultimately, your goal is to build a string consisting of the literal "boxy" and the contents of the variable clsuffix. That is done with "boxy" + clsuffix in JavaScript:
$("#box").wrap('<div class="boxy' + clsuffix + '" />');If you were just trying to insert the variable into the string (such as you might do in a PHP double-quoted string), it won't be interpreted by JavaScript:
// Doesn't work in JavaScript: $("#box").wrap('<div class="boxyclsuffix" />');更多推荐
class,变量,clsuffix,variable,电脑培训,计算机培训,IT培训"/> <meta name="de
发布评论