简体   繁体   English

通过使用html自定义属性值将类名称添加到html元素

[英]Add class name to a html element by using html custom attribute value

I have some html elements with custom attributes 我有一些带有自定义属性的html元素

<div menuitemname="my-media" class="panel panel-default">
//some code here
</div>

i wonder if there is a way (js will be nice) to add the value, of custom attribute "menuitemname", to my element class. 我想知道是否有一种方法(js会很好)将自定义属性“ menuitemname”的值添加到我的元素类中。 something like this 像这样的东西

<div menuitemname="my-media" class="panel panel-default my-media">
//some code here
</div>

on my entire site 在我的整个网站上

Any idea?? 任何想法??

Using jQuery you can do the following: 使用jQuery,您可以执行以下操作:

$('.panel').addClass(function(){
    return $(this).attr('menuitemname');
});

This will use the callback-function of addClass() to add the value of the attribute menuitemname 这将使用addClass()的回调函数添加属性menuitemname的值。

Example


Reference: 参考:

addClass() addClass()

attr() attr()

演示: http//jsfiddle.net/JmHpC/106/

    $('[menuitemname="my-media"]').addClass('my-media');

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

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