jQuery 如何将 data-toggle="dropdown" 添加到锚点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13358138/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:33:34  来源:igfitidea点击:

How can you add data-toggle="dropdown" to an anchor

jquery

提问by Paul Browne

So I have an anchor which looks like

所以我有一个锚看起来像

<a href="http://something.com/something/" class="dropdown-toggle">something</a>

and I want to add

我想补充

data-toggle="dropdown"

so that it ends up looking like

所以它最终看起来像

<a href="http://something.com/something/" class="dropdown-toggle" data-toggle="dropdown">something</a>

I tried

我试过

$(".dropdown-toggle").data("toggle", "dropdown");

But it isnt adding anything cheers

但它并没有增加任何欢呼声

回答by Adil

You can use attr()jquery method to add the attribute.

您可以使用attr()jquery 方法添加属性。

Live Demo

现场演示

$(".dropdown-toggle").attr("data-toggle", "dropdown");