如何在.wrap()里面使用变量(how to use variable inside .wrap())

编程入门 行业动态 更新时间:2024-10-28 00:14:13
如何在.wrap()里面使用变量(how to use variable inside .wrap())

这可能听起来很傻但我在这段代码中打印变量时遇到问题 -

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

本文发布于:2023-08-02 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1372243.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   如何在   wrap   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!