留言板

How to put the attribute "data-userId"

l ll,修改在9 年前。

How to put the attribute "data-userId"

Regular Member 帖子: 158 加入日期: 11-12-12 最近的帖子
Hi!

var el = A.Node.create('<div></div>');
el.set('id',userId);
el.set('data-userId',userId);
el.setData('data-userId',userId);


3 and 4 lines not work
thumbnail
Kyle Joseph Stiemann,修改在9 年前。

RE: How to put the attribute "data-userId"

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
Hi,
Try using element.setAttribute() instead of Node.set():

el.setAttribute('data-userId',userId);

- Kyle
l ll,修改在9 年前。

RE: How to put the attribute "data-userId"

Regular Member 帖子: 158 加入日期: 11-12-12 最近的帖子
I can not mix aui and pure js.
A.Node.create('<div data-userid="</code></pre><br /><img alt=" emoticon" src="@theme_images_path@/emoticons/smile.gif"></div>
thumbnail
Evan Thibodeau,修改在9 年前。

RE: How to put the attribute "data-userId"

New Member 帖子: 14 加入日期: 12-3-29 最近的帖子
Instead of using ".set" try using ".attr" like this:

el.attr('data-userId', userId);
thumbnail
Kyle Joseph Stiemann,修改在9 年前。

RE: How to put the attribute "data-userId"

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
l ll:
I can not mix aui and pure js.
A.Node.create('<div data-userid="</code></pre><br /><img alt=" emoticon" src="@theme_images_path@/emoticons/smile.gif"></div>
<br>Why not?<br><br><a href="http://jsfiddle.net/qypt9tcx/1/">This code works perfectly for me by the way:</a><br><br><span style="font-family: Courier New">var el = A.Node.create('&lt;div&gt;&lt;/div&gt;');<br>el.set('id',userId);<br>el.setAttribute('data-userId',userId);</span><br><br>- Kyle
thumbnail
David H Nebinger,修改在9 年前。

RE: How to put the attribute "data-userId"

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
Wait, isn't the setData() and getData() methods supposed to be preferred over direct attribute access? The whole point was to treat the attribs as keyed data and using the data methods enforces this pattern.

So in that case the OP should use el.getData("userId") and el.setData("userId", userId).
l ll,修改在9 年前。

RE: How to put the attribute "data-userId"

Regular Member 帖子: 158 加入日期: 11-12-12 最近的帖子
Kyle Joseph Stiemann

Excuse me. It appeared that I use AUI 1.5 and there is no setAttribute there. At the same time you showed an instance of AUI 2.0. That is why we did not understand each other.
thumbnail
Kyle Joseph Stiemann,修改在9 年前。

RE: How to put the attribute "data-userId"

Liferay Master 帖子: 760 加入日期: 13-1-14 最近的帖子
When I test the code with AlloyUI 1.5 it also works. Perhaps there's a js error in your code.

@David,
The docs for AlloyUI 1.5 recommend Node.setAttribute() for custom attrs, and specify that Node.setData() will not affect the DOM node. I don't know if that changed in other versions though.

- Kyle
thumbnail
David H Nebinger,修改在9 年前。

RE: How to put the attribute "data-userId"

Liferay Legend 帖子: 14919 加入日期: 06-9-2 最近的帖子
@Kyle: Nuts

emoticon

Actually I had gotten there from a different path, was working through the source for the aui:nav-item tag where the getData() is used vs getAttribute() and online stuff saying it's preferred because it clearly signifies intents...

Ah, well, thanks for following up.
l ll,修改在8 年前。

RE: How to put the attribute "data-userId" (答复)

Regular Member 帖子: 158 加入日期: 11-12-12 最近的帖子
Thanks for the answer.

Will be correct:


getData('userId');
and
setData('userId',1111);