javascript 如何在 jquery 中连接两个变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12604230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 16:38:32  来源:igfitidea点击:

How can you concatenate two variables in jquery?

javascriptjquerystring

提问by wcdomy

Can I do the following to concatenate two variables into one? Or there is a better way to do it?

我可以执行以下操作将两个变量连接为一个吗?或者有更好的方法来做到这一点?

   $('.row-add').live("click", function () {
          var newContent = $("<span>Example data</span>"+"");
          var newContent2 = $("<span>New Project</span>");
          var content = newContent+newContent2;
          $(this).closest("td").append(content);
        });

回答by aziz punjani

You can use .add

您可以使用 .add

content = newContent.add( newContent2 ); 

回答by moonwave99

If you use +between jQuery objects, you'll get the concatenation of String representations [fiddle].

如果您+在 jQuery 对象之间使用,您将获得字符串表示 [ fiddle]的串联。

If you want to append multiple elements, just do it.

如果你想追加多个元素,就去做吧。

$('.row-add').live("click", function () {
       var newContent = $("<span>Example data</span>"+"");
       var newContent2 = $("<span>New Project</span>");
       $(this).closest("td").append(newContent).append(newContent2);
});

回答by Aslam Khan

          var data = '<h3>' + name + '</h3>' + '<p>' + note + '</p>';