原生js 如何在原有标签里拼接字符串append

原生js 如何在原有标签里拼接字符串append,第1张

document.getElementById("myList").appendChild(str)

看这个链接:网页链接

希望可以帮到你,望采纳~

<script>

window.onload=function(){

var btn=document.createElement("button")

btn.innerHTML="btn"

document.body.appendChild(btn)

}

</script>

或者

<script>

window.onload=function(){

document.body.innerHTML+="<button>btn</button>"

}

</script>

JS中有三种字符串连接方式:

第一种方法 , 用连接符“+”把要连接的字符串连起来:

str="a"

str+="b"

第二种方法,  以数组作为中介用 join 连接字符串:

var arr=new Array()

arr.push(a)

arr.push(b)var str=arr.join("")

第三种方法,  利用对象属性来连接字符串:

function stringConnect(){    this._str_=new Array()

}

stringConnect.prototype.append=function(a){    this._str_.push(a)

}

stringConnect.prototype.toString=function(){    return this._str_.join()

}    var mystr=new stringConnect

mystr.append("a")   var str=mystr.toString()

JS中三种字符串连接方式的性能比较:

第一种,方法毫无疑问是最便捷快速的,如果只连接100个以下的字符串建议用这种方法最方便;

第二种,这种方法要比第一种消耗更少的资源,速度也更快;

第三种,方法加入了随机参数,应该是避免了缓存的影响的。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11931235.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存