创建使用jQuery的新元素的prefered方式 [英] The prefered way of creating a new element with jQuery
本文介绍了创建使用jQuery的新元素的prefered方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2种方式,我可以创建一个< DIV>
使用的jQuery
I've got 2 ways I can create a <div>
using jQuery
.
或者:
var div = $("<div></div>");
$("#box").append(div);
或者
$("#box").append("<div></div>");
什么是使用第二种方式以外重用性的弊端?
What are the drawbacks of using second way other than re-usability?
推荐答案
第一个选项为您提供更多的flexibilty:
The first option gives you more flexibilty:
var $div = $("<div>", {id: "foo", class: "a"});
$div.click(function(){ /* ... */ });
$("#box").append($div);
当然,的.html('*')
覆盖内容,而 .append('*')
没有,但我想,这不是你的问题。
And of course .html('*')
overrides the content while .append('*')
doesn't, but I guess, this wasn't your question.
另外一个好的做法是$ P $与 $
pfixing您的jQuery变量:结果
是否有背后使用$在jQuery的变量
Another good practice is prefixing your jQuery variables with $
:
Is there any specific reason behind using $ with variable in jQuery
这篇关于创建使用jQuery的新元素的prefered方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文