嗯……
首先你应该想想为什么你认为 jQuery 很好,但你不知道用什么论据来说服别人。也许你应该先说服自己。 ;)
无论如何,jQuery 只是另一个框架。你应该使用它最擅长的。如果您打算将它仅用于基本的 DOM 处理,请忘记它!学会正确使用js就可以了!
考虑一下这个 HTML:
<body>
<div style="width: 400px; height: 400px; background-color: red">
<div style="width: 400px; height: 400px; background-color: red">
</div>
<script type="text/javascript">
function test1 (){
document.body.innerHTML = ""
var div = document.createElement("div");
document.body.appendChild(div);
$(div).width("400px").height("400px").css("background-color", "red");
}
function test2 (){
document.body.innerHTML = ""
var div = document.createElement("div");
document.body.appendChild(div);
$(div).width("400px");
$(div).height("400px");
$(div).css("background-color", "red");
}
function test3 (){
document.body.innerHTML = ""
var div = document.createElement("div");
document.body.appendChild(div);
div.style.width = "400px";
div.style.height= "400px";
div.style.backgroundColor = "red";
}
function test4 (){
document.body.innerHTML = ""
var div = document.createElement("div");
document.body.appendChild(div);
div.setAttribute("style", "width: 400px; height: 400px; background-color: red");
}
</script>
</body>
将其放入带有 jquery 的 hml 文件中,<head>
然后,打开 firebug 并运行以下代码:
控制台.profile();
测试1();
控制台.profileEnd();
console.profile();
test2();
console.profileEnd();
console.profile();
test3();
console.profileEnd();
console.profile();
test4();
console.profileEnd();
亲自查看结果!
但是,如果您想对 DOM 进行一些复杂的操作,需要您进行多次循环或找出 DOM 上的元素是否符合某些条件,您可以考虑使用 jQuery,因为它会为您执行此过程。
无论如何,请记住,没有什么比使用您在代码中保留的引用来控制 DOM 更好的了。它的速度更快,对其他人来说更具可读性。谷歌“js 最佳实践”。
我将 jQuery 用于复杂的事情,这将花费我太长时间来实现,并且最终会得到类似于 jQuery 代码的东西。这种情况下,使用它是有意义的!
玩得开心!