我在javascript的开头有几个jquery选择器。
jQuery('.enter').show();
jQuery('.btnsecond').show();
jQuery('#id div.my-class').hide();
jQuery('.classes p:last').hide();
此jquery代码在页面的任何重新加载时运行...
我认为它会导致一个选择正确DOM的过程,哪个解决方案可以简化或优化这个加载过程?也许使用Css。不同地找到dom。
感谢。
答案 0 :(得分:1)
jQuery('.enter, .btnsecond').show();
jQuery('#id div.my-class, .classes p:last').hide();
答案 1 :(得分:1)
由于您没有在这些javascript / jQuery语句中应用编程逻辑,因此应用这些初始条件的最快方法是在样式表中使用CSS规则设置它们,而不是使用javascript。这样它们将在页面显示之前立即应用,而不是通过javascript设置,javascript必须等到DOM加载才能运行。
.enter, .btnsecond {display: block;}
#id div.my-class, .classes p:last {display: none;}
答案 2 :(得分:1)
要添加到mgraphs代码,您可以在选择器中指定上下文。这意味着您指定项目的包含元素,以便它仅搜索该元素的容器。这减少了dom遍历。
$exampleContainerDiv = $("#ContainerDivForSelectorsBelowExample");
jQuery('.enter, .btnsecond', $exampleContainerDiv).show();
jQuery('#id div.my-class, .classes p:last', $exampleContainerDiv).hide();