简体   繁体   中英

on Jquery template how do i set css class dynamically?

I'm using asp.net web api project and using jquery template in that.

in ready
$('a').click(function () {
            debugger;
            // remove the selected class from all anchors
            $('.row a').removeClass('selected');

            // Add the selected class to the currently clicked anchor
             $(this).addClass('selected');            
        });

<div class="row">
            <div class="span3">
                <!-- Added dynamic data from script GroupTypeTemplate -->              
            </div>
</div>
<script id="GroupTypeTemplate" type="text/html">
                <nav id="options" class="work-nav">
                    <ul id="filters" class="option-set" data-option-key="filter">                                            
                         <li>                            
 <a onclick="getGroupById('${Id}')"  data-option-value="*">${TypeName}</a>
                         </li>
                    </ul>
                </nav>                 
                </script>

I want to set that class attribute dynamically as per which menu is currently active

But how to check which id is currently active accordingly I can set class="selected"

What is the syntax in jquery template ?

this will put the selected attr only to the a that was clicked.

$("a").click(function(){
    $("a").removeClass("selected");
    $(this).addClass("selected");
});

to get script to work on dynamically added content you need to put the event on the document like this

$(document).on('click', 'a', function(){
    //remove, add here
});

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