[英]How much I should really worry when I use jquery to change elements in dom
How much should I worry and checking things before I switch something in dom. 在切换dom之前,我应该担心多少并检查一下。 for example I have 2 li's When one is pressed then it should change color and his siblings should change to other color. 例如,我有2个li's。当按下一个li时,它应该更改颜色,而他的兄弟姐妹应该更改为其他颜色。 Should I really be concerned to check if his siblings are already in the color they should be?? 我真的应该考虑检查他的兄弟姐妹是否已经处于应有的颜色? code its here" 在此处编码”
$(document).ready(function(){
var $header= $(".header");
var $list = $header.find("ul a");
var $li= $list.find("li");
function changeColor()
{
var lightblue = '#A3A3F3';
var darkblue = '#140C49';
var white = '#000000';
$(this).css('background-color',darkblue).css('color','white');
$(this).siblings().css('backgroundcolor',lightblue).css('color','black');
}
$li.on('click',changeColor);
});
Even If i had like 10 lis... Is Jquery really compressing the computer. 即使我有10 lis ... Jquery确实在压缩计算机。
Instead of worrying if your DOM changes might be too much for your computer (they are not), try to make your code more readable. 不必担心您的DOM更改对您的计算机来说可能太大(不是),而请尝试使代码更具可读性。
Working with CSS classes instead of defining all style properties in Javascript will make your function a lot easier to understand. 使用CSS类而不是使用Javascript定义所有样式属性将使您的函数更容易理解。
$(function () { function markActive() { $(this).closest(".header").find("li.active").removeClass("active"); $(this).addClass("active"); } $(".header").on("click", "li", markActive); $("#timing").click(function () { var $lis = $(".header li"), i, t = Date.now(); for (i = 0; i < 10000; i++) { $lis.each(markActive); } console.log("done (10,000 x 4 iterations in " + (Date.now() - t) + " ms)"); }); });
.header ul { list-style: none; margin: 0; padding: 0; } .header li { background-color: #A3A3F3; color: black; float: left; width: 50px; margin: 2px; padding: 3px; cursor: pointer; } .header li.active { background-color: #140C49; color: white; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="header"> <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> <li>Item 4</li> </ul> </div> <button id="timing">Timing</button>
To make a point, I've added a small timing function. 为了说明这一点,我添加了一个小的计时功能。 On my machine it runs the markActive
function 40,000 times in 900ms, each time marking a different <li>
as active. 在我的机器上,它在markActive
运行40,000次markActive
函数,每次将不同的<li>
标记为有效。 That's about 22 microseconds (!) per function call. 每个函数调用大约需要22微秒(!)。
Your values will be a bit different, but the important point is - it takes insignificant time to do simple DOM traversal and simple DOM changes in jQuery. 您的值将有所不同,但重要的一点是-在jQuery中进行简单的DOM遍历和简单的DOM更改花费的时间很少。
Worry about stuff that takes noticeable time per iteration. 担心每次迭代都需要花费大量时间的东西。 The blink of an eye takes 300 microseconds. 眨眼需要300微秒。 Everything less than 50ms is not really noticeable by humans. 小于50毫秒的所有内容都不是人类真正注意到的。 In 50ms our function has run more than 2,000 times. 在50毫秒内,我们的函数已运行2000多次。 And I say that knowing that I could optimize the code even more, easily making it twice as fast at least . 我说,知道我可以进一步优化代码,轻松地使其至少快两倍。 It just doesn't matter. 没关系。
Measure and then optimize the actual slow parts of your code. 测量然后优化代码的实际慢部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.