javascript 使用 jquery 添加数据工具提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add data-tooltip with jquery
提问by Jim22150
I'm trying to add an attribute to an element with jQuery; since my markup is created on-the-fly I can't directly add the attribute. Why is the below script not adding the attribute? Why is this fiddle not functioning as desired? Here is the fiddleI am trying to get working with some sample markup included.
我正在尝试使用 jQuery 向元素添加属性;由于我的标记是即时创建的,因此我无法直接添加该属性。为什么下面的脚本没有添加属性?为什么这个小提琴不能按预期运行?这是我正在尝试使用包含的一些示例标记的小提琴。
// set the tooltip content
jQuery('li#menu-item-75 a:hover:before').prop('tooltipText', 'w00t');
jQuery('li#menu-item-75 a:hover:after').prop('tooltipText', 'w00t');
采纳答案by Blender
You'll have to set the data attribute with .attr()
in order for the DOM attribute to actually be affected:
您必须设置 data 属性,.attr()
以便实际影响 DOM 属性:
jQuery('li#menu-item-75 a').attr('data-tooltip', 'w00t');
:hover
and :before
/:after
don't work with jQuery selectors.
:hover
和:before
/:after
不适用于 jQuery 选择器。