如何使用 jquery 连接 html 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14225219/
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-08-26 13:22:11  来源:igfitidea点击:

how to concatenate html strings using jquery

jquery

提问by neelmeg

How to perform the below operation with jQuery without using '+' sign?

如何在不使用“+”号的情况下使用 jQuery 执行以下操作?

var appendToResult = "<h4>Whoops, we noticed something.</h4><ul>";

    appendToResult = appendToResult+"<li><p>Please enter your Current Password.</p></li><li><p>Please enter a New Password.</p></li>";

回答by Jason

If you're just looking for a way to concatenate strings without using a plus sign, here's a way, no jQuery required:

如果您只是在寻找一种不使用加号连接字符串的方法,这里有一种方法,不需要 jQuery:

var arr = ['text thing 1', 'other text', 'more text', 'etc'];
var str = arr.join(' ');

If you have a jQuery object you're trying to append text to, you can do that like this:

如果您有一个 jQuery 对象要添加文本,您可以这样做:

var myObj = $('.myclass');
myObj.append('my appended text here');

回答by slashingweapon

Or...

或者...

var appendToResult = "<h4>Whoops, we noticed something.</h4><ul>";
appendToResult = appendToResult.concat("<li><p>Please enter your Current Password.</p></li><li><p>Please enter a New Password.</p></li>");

What exactly is the source of your displeasure with the gentle plus sign, which never did anyone harm?

您对从未伤害过任何人的温柔加号感到不满的原因究竟是什么?