GVKun编程网logo

javascript – 使用JS querySelector的性能[closed]

16

本文将为您提供关于javascript–使用JSquerySelector的性能[closed]的详细介绍,同时,我们还将为您提供关于IE8下关于querySelectorAll()的问题_javas

本文将为您提供关于javascript – 使用JS querySelector的性能[closed]的详细介绍,同时,我们还将为您提供关于IE8下关于querySelectorAll()的问题_javascript技巧、javascript – jQuery .selector属性已删除,解决方法?、javascript – jQuery find .Selector但不是嵌套的选择器(.selector .selector)、javascript – jquery selector / checked属性在IE7中不更新的实用信息。

本文目录一览:

javascript – 使用JS querySelector的性能[closed]

javascript – 使用JS querySelector的性能[closed]

在Web浏览器中使用 JavaScript时,以下内容之间存在任何性能差异:

现有getElementById

document.getElementById("elem");

查询选择器使用#id

document.querySelector("#elem");

查询选择器使用[id = elem]

document.querySelector("[id=elem]");

我假设第一个将是最快的(只需要查找具有ID的元素).最后一个看起来像坏习惯.我喜欢第二个使用querySelector的一切使得代码容易阅读.

有什么建议么?

解决方法

首先,
document.querySelector("#elem");

有一个优点,事实上,与document.getElementId不同,它可以返回类.但是,由于只返回具有该类名称的第一个对象,所以这样做的实用性远远减少,因此如果您没有特别寻找具有该类名称的第一个对象,那么您也可以使用一个id.如果你使用,

document.querySelectorAll

但是,我相信(我可能是错误的),它返回所有具有该类名称的数组作为数组,其中常规querySelector相当于querySelectorAll [0].另外一个优点是可以通过它运行css3查询,这可能是非常有用的.

其次,

document.getElementById("elem");

在查询选择器方面具有非常好的优势,因为它几乎是快5倍,所以如果你坐在那里有几千行代码,并且想要优化代码,那么getElementById就是要走的路.

最后,

document.querySelector("[id=elem]");

我个人认为,在任何情况下都不需要使用它.如果你需要一个querySelector,为什么不使用#?这完全等同于你的第一个querySelector的例子,但它有很多无用的特征.

编辑:只是为了清楚,总而言之,你最好使用document.getElementById.

IE8下关于querySelectorAll()的问题_javascript技巧

IE8下关于querySelectorAll()的问题_javascript技巧

当用querySelector()或querySelectorAll()查找类似name="2nd_btn"的元素时,FF,chrome和IE8都会报错。
FF,chrome报的错是一样的,如下所示:
Error: uncaught exception: [Exception... "An invalid or illegal string was specified" code: "12" nsresult: "0x8053000c (NS_ERROR_DOM_SYNTAX_ERR)" location: ".../test/qsa.html Line: 18"]
IE8的报错提示:行: 18 错误: 参数无效。
一寻思,name的值是以数字开头的,把数字去掉或修改后,就能取到了。
这就告诉我们,一般可自定义标签的属性值时,属性值不能以数字开头,也不能包含$,^等不常用的字符。
在HTML页面开始一定要记得声明。

测试代码:


复制代码 代码如下:






This is a sample warning
This is a sample error


This is another sample warning
This is another sample error

<script> <BR>var a = document.querySelectorAll("[name=warns]") <BR>alert(a.length)//输出:2 <BR>var b = document.querySelector("[id=3err]") <BR>alert(b.tagName)//报错 <BR>var c = document.querySelectorAll("[name=1err]") <BR>alert(c.length)//报错 <BR></script>


javascript – jQuery .selector属性已删除,解决方法?

javascript – jQuery .selector属性已删除,解决方法?

我试图让选择器用于调用当前脚本,但当然我需要的属性是 removed for some reason.

这有解决方法吗?这基本上就是我想要完成的事情:

(function($) {
    $.fn.myplugin = function(options) {
        return this.each(function() {
            console.log($(this).selector);
        });
    }
}(jQuery));

//Somwehere else:

$('.theClassISelected').myplugin();  //Should console.log('.theClassISelected')

我需要在控制台中看到.theClassISelected(或者我用来调用函数的某种形式的原始选择器),但由于选择器属性已从jQuery中删除,因此它不再可能.

我不明白为什么它被删除了 – 我已经用Google搜索了这个问题一段时间了,我看到的是2011-2012推荐选择器属性的StackOverflow答案.我想这在某些方面很有用,但现在不行了?

解决方法

从jQuery文档:

Plugins that need to use a selector should have the caller pass in the selector as part of the plugin’s arguments during initialization.

http://api.jquery.com/selector/

另外,文档还提到选择器属性不可靠,因为“因为后续的遍历方法可能已经改变了集合”.

javascript – jQuery find .Selector但不是嵌套的选择器(.selector .selector)

javascript – jQuery find .Selector但不是嵌套的选择器(.selector .selector)

我想找到所有匹配选择器的元素,但是如果它已经包含在匹配元素中则不会.

$(‘#container’).find(‘.child’).not(‘.child .child’);

请注意,.child元素不是必需的直接后代.

为什么这不起作用?

我想选择所有出现在$(‘#container’)中的元素.find(‘.child’)但排除/ filter()任何将在这里的元素$(‘#container’).find(‘. child .child’)因为它的一个祖先是.child

var children = $('#container').find('.child').filter(function (i, el) {
    return !$(el).closest('.child').length;
});

由于某种原因,这不起作用JSFIDDLE

片段改编自@RonenCypis的回答

var selector = ' .child ';
var children = $('#container').find(selector).filter(function(i, el) {
  return !$(el).closest(selector).length;
});
children.css('background-color', 'blue');
#container div {
  float: left;
  display: inline-block;
  width: 50px;
  height: 50px;
  color: #fff;
  margin: 10px;
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div>one
    <div>one one</div>
  </div>
  <div>two
    <div>two one</div>
  </div>
  <div>three</div>
  <div>four
    <div>four one</div>
  </div>
</div>

解决方法:

您可以使用parents而不是最近来查找当前元素的祖先.最近的匹配除了祖先之外的当前元素.

var selector = ' .child ';
var children = $('#container').find(selector).filter(function(i, el) {
  return !$(el).parents(selector).length;
});
children.css('background-color', 'blue');
#container div {
  float: left;
  display: inline-block;
  width: 50px;
  height: 50px;
  color: #fff;
  margin: 10px;
  background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div>one
    <div>one one</div>
  </div>
  <div>two
    <div>two one</div>
  </div>
  <div>three</div>
  <div>four
    <div>four one</div>
  </div>
</div>

javascript – jquery selector / checked属性在IE7中不更新

javascript – jquery selector / checked属性在IE7中不更新

我有一个由链接调用的函数,它应该检查特定div中存在的各种复选框(传递给函数.)适用于除IE之外的所有浏览器(7.)据我所知.attr(‘checked’,‘checked’是使用 jquery 1.5.1执行此操作的正确方法

function selectall(industry){
    $("#"+industry+"Apps :checkBox").attr('checked','checked');
    $("#"+industry+"Apps :checkBox").change(); /* used for jqtransform plugin */
    }

有没有我缺少的东西或更好的方法来做到这一点适用于所有浏览器?我真的不在乎取消选择,只是选择.

HTML看起来像

<div id = 'HealthcareApps'>
<div>
    <a href = "javascript:selectall('Healthcare');">Select all</a>
</div>
<ul>
    <li><label for="id_Healthcare_0"><input type="checkBox" name="Healthcare" value="1" id="id_Healthcare_0" /> Simple Messaging</label></li>
    <li><label for="id_Healthcare_1"><input type="checkBox" name="Healthcare" value="2" id="id_Healthcare_1" /> Mobile Alerts and Alarms</label></li>
    <li><label for="id_Healthcare_2"><input type="checkBox" name="Healthcare" value="3" id="id_Healthcare_2" /> Patient Identification / Drug Conflicts</label></li>
</ul>

(是的,我知道内联样式是一个可怕的想法.如果我能在IE中使用此链接,我也会解决这个问题.)

解决方法

你可以尝试这样的事: js fiddle demo

<div id = 'HealthcareApps'>
<div>
    <a href="#" id="selectall">Select all</a>
</div>
<ul>
    <li><label for="id_Healthcare_0"><input type="checkBox" name="Healthcare" value="1" id="id_Healthcare_0" /> Simple Messaging</label></li>
    <li><label for="id_Healthcare_1"><input type="checkBox" name="Healthcare" value="2" id="id_Healthcare_1" /> Mobile Alerts and Alarms</label></li>
    <li><label for="id_Healthcare_2"><input type="checkBox" name="Healthcare" value="3" id="id_Healthcare_2" /> Patient Identification / Drug Conflicts</label></li>
</ul>
</div>

jQuery的:

$('#selectall').click(function(e) {
    e.preventDefault();
    var industry = $(this).attr("class");
    $("#" + industry + "Apps :checkBox").attr('checked','checked');
});

并且,要打开和关闭:

$('#selectall').click(function(e) {
    e.preventDefault();
    var industry = $(this).attr("class");
    var checkBox = $("#" + industry + "Apps :checkBox");
    checkBox.attr('checked',!checkBox.attr('checked'));
});

js fiddle toggle demo

今天关于javascript – 使用JS querySelector的性能[closed]的介绍到此结束,谢谢您的阅读,有关IE8下关于querySelectorAll()的问题_javascript技巧、javascript – jQuery .selector属性已删除,解决方法?、javascript – jQuery find .Selector但不是嵌套的选择器(.selector .selector)、javascript – jquery selector / checked属性在IE7中不更新等更多相关知识的信息可以在本站进行查询。

本文标签:

上一篇javascript – 没有找到karma插件依赖项(没有找到插件的web页面)

下一篇javascript – 什么时候实际使用ChannelUrl?(js什么时候会用到闭包)