Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix data(action) to attr(data-action) #30

Merged
merged 1 commit into from
Mar 18, 2016

Conversation

lyt9304
Copy link
Contributor

@lyt9304 lyt9304 commented Mar 18, 2016

将data("action")变成attr("data-action"),防止由于data调用和attr调用混合导致的jquery缓存池存取值不一。

@cssmagic
Copy link
Owner

谢谢发现并修复此问题。经过这个修改,Action 对 jQuery 的兼容性更好了。

这个问题的具体解释是这样的:

jQuery 的 .data() API 是基于自己的数据 store 的,因为这个 API 要远远早于 HTML 自定义属性(Dataset)规范的诞生。但由于这两者的功能类似,从某个版本开始,jQuery 也会兼顾元素的 HTML 标签内的 data-* 属性。不过,jQuery 只会在第一次用 .data() 读取某键值时会看元素身上同名的 HTML 自定义属性(并把读取结果写入自己的数据 store),此后对该键的读取都是走它自己的数据 store 的。另外,jQuery 用 .data() 写数据的操作则是一直跟 HTML 属性完全无关的。

这种行为反映到 Action 身上,就是在用 .data() 取了一次 action 名后,不论元素的 data-action 属性改成什么,再次用 .data() 取到的 action 名仍将是第一次取到的结果。

因此,对 jQuery 用户(以及 Action 的内部实现)来说,要修改或读取某个元素身上的 action 名,应该总是使用 .attr() 这个 API 来处理。

cssmagic added a commit that referenced this pull request Mar 18, 2016
fix data(action) to attr(data-action)
@cssmagic cssmagic merged commit 9417655 into cssmagic:master Mar 18, 2016
@cssmagic
Copy link
Owner

最好再补充个单元测试。 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants