简体   繁体   English

jQuery向特定的li id和class添加一个简单的data属性

[英]jQuery add a simple data attribute to specific li id and class

This probably won't excite anyone too much but I thought I'd ask as I've tried a number of ways to do this. 这可能不会让任何人感到兴奋,但是我想问一下,因为我尝试了多种方法来做到这一点。 I am just trying to add a data attribute to a menu item which activates the drop-down facility in the nav in Wordpress. 我只是想将数据属性添加到菜单项中,以激活Wordpress中导航中的下拉功能。 (UIKit CSS/JS) The HTML for the element is (UIKit CSS / JS)元素的HTML是

 <li class="uk-parent" data-uk-dropdown="{mode:'click'}">

The uk-parent class has already been added as have the sub ul classes necessary for it to work, leaving just the data-uk-dropdown mode to be added. uk-parent类已经被添加,它具有工作所需的sub ul类,仅添加了data-uk-dropdown模式。 I've tried addClass, appendTo and a few others but can't get it to register. 我试过addClass,appendTo和其他几个,但是无法注册。 The id of the li is "menu-item-140" and I only need to target this one, not any others. li的ID是“ menu-item-140”,我只需要将此目标定位,无需其他任何目标。 So typically I have at the moment, going back to addClass (but it's not really a class.) as below: 因此,通常我现在会回到addClass(但它实际上不是一个类),如下所示:

jQuery(document).ready( function($) {
$("#menu-item-140.uk-parent").addClass("data-uk-dropdown='{mode:'click'}");
$('.sub-menu').addClass('uk-dropdown uk-dropdown-navbar');
});

before trying to add the data-uk-dropdown, the .sub-menu classes showed up fine, but as it is at present this actually makes the sub item disappear from the menu altogether, leaving just the parent items. 在尝试添加data-uk-dropdown之前,.sub-menu类显示正常,但就目前而言,这实际上使子项从菜单中完全消失,仅留下了父项。

Any Tips welcome, Thanks 欢迎任何提示,谢谢

addClass用于添加类,以实际添加属性,使用attr

$("#menu-item-140.uk-parent").attr("data-uk-dropdown", "{mode:'click'}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM