简体   繁体   English

jQuery:如何向页面元素添加有用的信息?

[英]jQuery: how can I add helpful information to my page elements?

I am tempted to make some custom attributes for some page elements to make selection easier, via the .attr(); 我很想通过.attr();为一些页面元素制作一些自定义属性,以便更容易地进行选择.attr(); function in jQuery. jQuery中的函数。

I figure that this is probably discouraged, as there is a lot of flexibility in jQuery and javaScript that, as a novice, I have not learned to employ yet to its full potential. 我认为这可能是气馁的,因为jQuery和javaScript有很多灵活性,作为一个新手,我还没有学会充分发挥它的潜力。

Other than making cryptic IDs or adding lots of classes, what are some ways to add more information to my divs and other page elements for easy selecting via the selector $() ? 除了制作神秘的ID或添加大量的类之外,还有哪些方法可以为我的div和其他页面元素添加更多信息,以便通过选择器$()轻松选择?

I'm mostly curious as to pros/cons, and what memory issues or speed issues result based on different approaches. 我对利弊很感兴趣,基于不同的方法会产生什么样的记忆问题或速度问题。

There isn't a problem at all creating custom attributes on your elements. 完全没有问题在元素上创建自定义属性。 Anyway, you should follow the html5 data- spec. 无论如何,你应该遵循html5 data-规范。

<div id="foo" data-info="hello" data-more="{'iam': 'anobject'}" />

The cool thing about using this is, jQuery (latest version) will automatically parse the data-x attributes and adds those into the .data() expando. 使用它的很酷的事情是,jQuery(最新版本)将自动解析data-x属性并将它们添加到.data() expando中。

$('#foo').data('info') === 'hello'
$('#foo').data('more').iam === 'anobject'

You are still able to run a selector like 你仍然可以运行像这样的选择器

$('div[data-info=hello]')

on your elements. 在你的元素上。

ref.: .data() , html5 data attributes 编号: 。数据() , HTML5数据属性

HTML5 introduces data-* attributes . HTML5引入了data-*属性 So if you are going to introduce custom attributes, I would at least stick to this specification. 因此,如果您要引入自定义属性,我至少会坚持使用此规范。

There is also the .data() method in jQuery for associating arbitrary data with elements. jQuery中还有.data()方法,用于将任意数据与元素相关联。 As long as you don't need to use the data in a selector, this is the preferred way. 只要您不需要在选择器中使用数据,这是首选方法。

jQuery plug ins usually use some already existing attributes like "rel" or "title" but jQuery also has the .data() method which stores information on an specific object in javascript. jQuery插件通常使用一些已经存在的属性,如“rel”或“title”,但jQuery也有.data()方法,它在javascript中存储特定对象的信息。 It acts like a log. 它就像一个日志。

http://api.jquery.com/jQuery.data/ http://api.jquery.com/jQuery.data/

Both of this techniques are acceptable and work well. 这两种技术都是可以接受的并且效果很好。

The first thing to be aware of is that the jQuery selectors are extremely powerful. 首先要注意的是jQuery选择器 非常强大。 You don't always need to select by an ID or a class - you can select by tag, by type, by relation to another selection (children/parents/siblings/etc). 您并不总是需要通过ID或类进行选择 - 您可以按标签,类型,与其他选择(儿童/父母/兄弟姐妹/等)的关系进行选择。 So, there may not be a need to add IDs or classes to your various elements in order to select them. 因此,可能不需要为各种元素添加ID或类以便选择它们。

With that said, don't avoid classes and IDs just for avoiding classes and IDs. 话虽如此,不要为避免类和ID而避免使用类和ID。 They exist for a reason, and that reason is to make selection of them straightforward and easy (whether it is with jQuery or CSS or whatever else). 它们存在是有原因的,其原因在于可以直接和简单地选择它们(无论是使用jQuery还是CSS或其他任何东西)。 If you remove classes and IDs, you may make your HTML look nicer, but your jQuery code may become convoluted and/or very inefficient. 如果删除类和ID,可能会使HTML看起来更好,但是jQuery代码可能会变得错综复杂和/或效率很低。 (For example, it is always faster to select an ID directly than select a parent component and find its children.) (例如,直接选择ID总是比选择父组件并找到其子组件更快。)

I don't think you should be discouraged to use attr(). 我不认为你应该气馁使用attr()。 It is a powerfull tool and really helps to add more information to your elements. 它是一个功能强大的工具,真正有助于为您的元素添加更多信息。 I have some projects using it to suport internal javascript frameworks and it turns my code clean and legible. 我有一些项目使用它来支持内部javascript框架,它使我的代码干净清晰。 And it has the advantage to be is a ease to work with selectors! 它的优势在于易于与选择器配合使用!

You should really be using only classes and IDs for the initial selection but here are some handy selectors you can use on top of this: 您应该只使用类和ID进行初始选择,但是这里有一些方便的选择器,您可以在此基础上使用:

http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

A unique ID is the quickest way to find an element through jQuery, so if the purpose of your temptation is to make your elements easier for jQuery to retrieve, it seems pointless. 一个唯一的ID是通过jQuery查找元素的最快方法,所以如果你的诱惑的目的是让你的元素更容易被jQuery检索,那么它似乎毫无意义。

I've used such techniques for adding attributes such as title or alt for better accessibility. 我已经使用这些技术添加title或alt等属性以获得更好的可访问性。

使用.data('key', 'val')设置数据和.data('key')来读取它。

我肯定会使用Data api( http://api.jquery.com/jQuery.data/

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

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