jQuery获取元素的属性“循环" [英] jQuery get element's attribute 'loop'

查看:75
本文介绍了jQuery获取元素的属性“循环"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用jQUery 1.6.2获取div元素的属性

I try to get an attribute of a div element using jQUery 1.6.2

html

<div id="scroller" loop="2"></div>

js

$('#scroller').attr('loop') 

上面的js总是返回undefined.

The above js would always return undefined.

我尝试将jquery升级到1.6.4,然后相同的js返回"loop"一词.这个循环属性是保留属性吗?

I try to upgrade jquery to 1.6.4, then the same js returns me the word 'loop'. Is this loop attribute a reserved attribute?

我这样做是因为使用库来滚动文本.http://remysharp.com/2008/09/10/the-silky-smooth-marquee/

I do this becoz of using a library for scrolling text.http://remysharp.com/2008/09/10/the-silky-smooth-marquee/

推荐答案

真正发生的是jQueryattr的实现. loop是媒体的布尔属性(<audio><video>)元素,因此它的存在表示音频或视频应循环播放.

What's really going on is jQuery's implementation of attr. loop is a boolean attribute for media (<audio> and <video>) elements, so its presence indicates that the audio or video should loop.

使用布尔属性时,指定属性名称本身以外的值是无效的. jQuery检查布尔属性时,它将返回属性本身的名称.在这种情况下,[loop]的值是其名称:"loop".

When boolean attributes are used, it is invalid to specify a value other than the attribute name itself. When jQuery checks a boolean attribute, it returns the name of the attribute itself. In this case [loop]'s value is its name: "loop".

除非您使用HTML5中指定的[data-]属性,否则不应向元素添加自定义属性:

You shouldn't be adding custom attributes to elements unless you're using the [data-] attributes specified in HTML5:

<div id="scroller" data-loop="2"></div>

jQuery支持使用data函数访问以下[data-]属性:

jQuery supports accessing these [data-] attributes with the data function:

$('#scroller').data('loop'); //returns "2"

这篇关于jQuery获取元素的属性“循环"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆