简体   繁体   中英

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

I have some html elements with custom attributes

<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. 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:

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

This will use the callback-function of addClass() to add the value of the attribute menuitemname

Example


Reference:

addClass()

attr()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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