如何将一些文本附加到变量中

How can I append some text to a variable?

本文关键字:变量 文本      更新时间:2023-09-26

我正在使用jquery,我需要将一些文本附加到一个变量:

$("document").ready(function(){
  var i="this is text";
$(i).append("aa");
  alert(i);
})

演示

您当然可以为这个创建一个jQuery插件

$.fn.appendString = function(str) {
    return this.selector + str;
}
$(document).ready(function(){
    var i = "this is text";
    alert( $(i).appendString("aa") );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


但它连接字符串

var i = "this is text";
i = i + "aa"; // it's now "this is textaa"

注意,$("this is text")并不是真正有效的东西,jQuery并没有真正处理字符串