admin管理员组文章数量:1023773
I'm having trouble updating elements from JavaScript. My project is fairly plex but here's a tiny piece of it:
myWebService(/*...*/)
.done(function (result) {
var newRows = $(rows).clone();
$('#drop-placeholder').after(newRows);
$(newRows).data('test', 'ZZZZZZZZZZZZZZZZZZZZZZZ');
}
Using Chrome, I can watch these statements execute. However, with newRows
in the watch window, I can see that the attribute never gets added/updated. And, sure enough, after the call is done, I don't see the specified attribute on my page.
Can anyone see what I might be missing. rows
(and therefore newRows
) represents any number of table row elements.
I'm having trouble updating elements from JavaScript. My project is fairly plex but here's a tiny piece of it:
myWebService(/*...*/)
.done(function (result) {
var newRows = $(rows).clone();
$('#drop-placeholder').after(newRows);
$(newRows).data('test', 'ZZZZZZZZZZZZZZZZZZZZZZZ');
}
Using Chrome, I can watch these statements execute. However, with newRows
in the watch window, I can see that the attribute never gets added/updated. And, sure enough, after the call is done, I don't see the specified attribute on my page.
Can anyone see what I might be missing. rows
(and therefore newRows
) represents any number of table row elements.
-
1
FYI,
newRows
is already a jQuery object. You don't have to pass it to jQuery again. What do yo mean by "I don't see the specified attribute on my page"? How are you trying to "see" it? – Felix Kling Commented Mar 27, 2013 at 22:50 -
Are you saying I can just do
newRows.data()
? I thought$()
gave me additional options. (I was trying to see it by examining the elements in Chrome.) – Jonathan Wood Commented Mar 27, 2013 at 22:52 -
Yep.
.clone
returns a jQuery object, as does$()
. If you already have a jQuery object, you don't have to pass it to$()
again (the same might be forrows
, but I don't know what it is). You usually do$('div')
instead of$($('div'))
right? :) – Felix Kling Commented Mar 27, 2013 at 23:03
2 Answers
Reset to default 9.data()
sets background data that is handled purely by jQuery - it does not set HTML attributes.
If you are trying to set an attribute such as data-test
in <div data-test="stuff"></div>
you need to use .attr("data-test", variable)
.
The setting the data
via jQuery .data()
does not set the attribute, only the underlying data store. If you want to change the attribute, you have to use .attr()
.
I'm having trouble updating elements from JavaScript. My project is fairly plex but here's a tiny piece of it:
myWebService(/*...*/)
.done(function (result) {
var newRows = $(rows).clone();
$('#drop-placeholder').after(newRows);
$(newRows).data('test', 'ZZZZZZZZZZZZZZZZZZZZZZZ');
}
Using Chrome, I can watch these statements execute. However, with newRows
in the watch window, I can see that the attribute never gets added/updated. And, sure enough, after the call is done, I don't see the specified attribute on my page.
Can anyone see what I might be missing. rows
(and therefore newRows
) represents any number of table row elements.
I'm having trouble updating elements from JavaScript. My project is fairly plex but here's a tiny piece of it:
myWebService(/*...*/)
.done(function (result) {
var newRows = $(rows).clone();
$('#drop-placeholder').after(newRows);
$(newRows).data('test', 'ZZZZZZZZZZZZZZZZZZZZZZZ');
}
Using Chrome, I can watch these statements execute. However, with newRows
in the watch window, I can see that the attribute never gets added/updated. And, sure enough, after the call is done, I don't see the specified attribute on my page.
Can anyone see what I might be missing. rows
(and therefore newRows
) represents any number of table row elements.
-
1
FYI,
newRows
is already a jQuery object. You don't have to pass it to jQuery again. What do yo mean by "I don't see the specified attribute on my page"? How are you trying to "see" it? – Felix Kling Commented Mar 27, 2013 at 22:50 -
Are you saying I can just do
newRows.data()
? I thought$()
gave me additional options. (I was trying to see it by examining the elements in Chrome.) – Jonathan Wood Commented Mar 27, 2013 at 22:52 -
Yep.
.clone
returns a jQuery object, as does$()
. If you already have a jQuery object, you don't have to pass it to$()
again (the same might be forrows
, but I don't know what it is). You usually do$('div')
instead of$($('div'))
right? :) – Felix Kling Commented Mar 27, 2013 at 23:03
2 Answers
Reset to default 9.data()
sets background data that is handled purely by jQuery - it does not set HTML attributes.
If you are trying to set an attribute such as data-test
in <div data-test="stuff"></div>
you need to use .attr("data-test", variable)
.
The setting the data
via jQuery .data()
does not set the attribute, only the underlying data store. If you want to change the attribute, you have to use .attr()
.
本文标签: javascriptUnable to Set Element Attribute Using jQuery data()Stack Overflow
版权声明:本文标题:javascript - Unable to Set Element Attribute Using jQuery data() - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745530049a2154707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论