jQuery获取元素的属性“循环" [英] jQuery get element's attribute 'loop'
问题描述
我尝试使用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/
推荐答案
真正发生的是jQuery
对attr
的实现. 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屋!