用 jQuery 编写 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5593012/
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 19:26:31 来源:igfitidea点击:
Writing HTML with jQuery
提问by Ronan
I am new to jQuery. I want to write the following HTML (along with the classes) using jQuery. How can I do this?
我是 jQuery 的新手。我想使用 jQuery 编写以下 HTML(以及类)。我怎样才能做到这一点?
<div class="phnbr">
<div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a>
</div>
</div>
回答by NKCSS
$('<div>').addClass('phnbr').append($('<div>').addClass('phtext').append('hi how are you, ').append($('<a>').attr({ target: '_blank', href: 'http://www.xyc.com'}).text('click here.')));
回答by Luca Matteis
$('<div class="phnbr"> \
<div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a> \
</div> \
</div>'); // bang done!
回答by Khez
Well you can just use append()or the other DOM Insertion Functions
$(document.body).append('<div class="phnbr"><div class="phtext">hi how are you, <a target="_blank" href="http://www.xyz.com">click here.</a></div></div>');