简体   繁体   English

当我使用jquery更改dom中的元素时,我应该真正担心多少

[英]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.

相关问题 Javascript 劫持,我应该担心什么时候以及担心多少? - Javascript hijacking, when and how much should I worry? 将jQuery / javascript事件附加到元素时,应如何验证页面DOM? - How should I verify the page DOM when attaching jQuery/javascript events to elements? 如何在鼠标悬停时使用JQuery更改DOM元素的背景颜色? - How should I use JQuery to change the background color of a DOM element on mouse over? 我可以在GWT中使用多少Java? - How much of Java can I really use with GWT? 我应该使用原始 js 还是 jquery 来定位 Vuejs 3 应用程序中的 DOM 元素? - Should I use raw js or jquery to target DOM elements in a Vuejs 3 app? Mootools,搜索dom以查看是否存在一个类,我是否应该担心每页多次执行该操作 - Mootools, searching dom to see if a class exists, should I worry about doing it multiple times every page 我应该担心javascript支持吗? - Should I worry about javascript support? Jquery / Javascript速度......我应该真的关心吗? - Jquery / Javascript speed… should I really care? ES6中的销毁。 我应该担心吗? - Destructuring in ES6. Should I worry? 我应该如何从我的JavaScript中引用DOM元素? - How should I reference DOM elements from my javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM