简体   繁体   English

选择元素| Javascript vs jQuery

[英]Selecting an Element | Javascript vs Jquery

Whats the difference between an element selected by Javascript and the same element selected with jQuery? 用Javascript选择的元素和用jQuery选择的相同元素有什么区别?

I ran into this problem, trying to select a Html5 data attribute from an element with jQuery, the dataset would return undefined even though it was clearly set. 我遇到了这个问题,尝试使用jQuery从元素中选择Html5数据属性,即使数据集已明确设置,数据集也将返回未定义。 Selecting the element with regular javascript works perfectly fine however. 但是,使用常规javascript选择元素可以很好地工作。 So what behind the scenes is different that stops this example from working. 因此,幕后情况有所不同,这使该示例无法正常工作。

 var jsSelect = document.getElementById("jsSelect"); alert(jsSelect.dataset.name); var jqSelect = $("#jsSelect"); alert(jqSelect.dataset.name); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="jsSelect" data-name="Javascript"></div> 

Whats the difference between an element selected by Javascript and the same element selected with jQuery? 用Javascript选择的元素和用jQuery选择的相同元素有什么区别?

There's no difference in the element , but jQuery gives you back the element wrapped in a jQuery object, whereas the DOM gives you a reference to the element itself. 元素没有什么区别,但是jQuery为您提供了包装在jQuery对象中的元素,而DOM为您提供了对元素本身的引用。 jQuery objects are sets of elements (since it lets you select more than one and treat them as a set). jQuery对象是元素 (因为它使您可以选择多个元素并将它们视为一组)。

To get the element itself from the jQuery object, you can index into it like an array: 要从jQuery对象获取元素本身,可以像数组一样对其进行索引:

var rawElement = jqSelect[0];
alert(rawElement.dataset.name);

 var jsSelect = document.getElementById("jsSelect"); snippet.log(jsSelect.dataset.name); var jqSelect = $("#jsSelect"); var rawElement = jqSelect[0]; snippet.log(rawElement.dataset.name); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="jsSelect" data-name="Javascript"></div> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

To deal with data-* attributes, you can also use jQuery's attr function: 要处理data-*属性,您还可以使用jQuery的attr函数:

alert(jqSelect.attr("data-name"));

 var jsSelect = document.getElementById("jsSelect"); snippet.log(jsSelect.dataset.name); var jqSelect = $("#jsSelect"); snippet.log(jqSelect.attr("data-name")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="jsSelect" data-name="Javascript"></div> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

You can use jQuery's data function, but it's important to read the documentation — data is not just a convenience wrapper for dataset . 可以使用jQuery的data功能,但它阅读的文档是很重要的- data 不仅仅是一个便利的包装dataset data manages a completely separate cache of data that is only initialized from data-* attributes (it doesn't update them). data管理一个完全独立的数据缓存,该缓存仅从data-*属性初始化 (不会更新它们)。

 var jsSelect = document.getElementById("jsSelect"); snippet.log(jsSelect.dataset.name); var jqSelect = $("#jsSelect"); snippet.log(jqSelect.data("name")); // Note that `data` doesn't change the attribute: jqSelect.data("name", "new name"); snippet.log("Attribute after changing with data: " + jqSelect.attr("data-name")); // But you can retrieve the updated info via `data` snippet.log("Data value after changing with data: " + jqSelect.data("name")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="jsSelect" data-name="Javascript"></div> <!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> <script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script> 

You may be wondering why, if the jQuery object is a set of elements, you can use attr to get the data-name attribute from an element — which element does it get the attribute from? 您可能想知道为什么,如果jQuery对象是一元素,则可以使用attr从元素中获取data-name属性-它从哪个元素中获取属性? jQuery's accessor functions like attr are assymetrical: If you're retrieving information, they retrieve it from the first element in the set; jQuery的attr之类的访问器函数是不对称的:如果您要检索信息,它们将从集合中的第一个元素中检索信息; if you're setting information ( jqSelect.attr("data-name", "newvalue") ), they set it on all of the elements in the set. 如果您要设置信息( jqSelect.attr("data-name", "newvalue") ),则它们将对集合中的所有元素进行设置。 Which sounds crazy, but works surprisingly well. 听起来很疯狂,但效果惊人。

In case of jQuery jqSelect is a jQuery object so it doesn't have the dataset property, but you can use the data api 如果是jQuery, jqSelect是jQuery对象,因此它没有dataset属性,但是您可以使用数据API

 var log = (function() { var $log = $('#log'); return function(msg) { $('<p/>', { text: msg }).appendTo($log) } })(); var jsSelect = document.getElementById("jsSelect"); log('native api:' + jsSelect.dataset.name); var jqSelect = $("#jsSelect"); log('jquery api:' + jqSelect.data('name')); var jqSelect = $("#jsSelect"); log('Using prop:' + jqSelect.prop('dataset').name); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="jsSelect" data-name="Javascript"></div> <div id="log"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM