将动态id分配给jquery

时间:2016-03-23 07:03:52

标签: javascript jquery

我有4个链接,它们有不同的ID,当我点击其中一个链接时,如何将不同的id分配到jquery?任何指导都非常感谢。谢谢!

HTML:

 <ul class="dropdown-menu">
         <li>@Html.ActionLink("Manage Site", "Index", "Site", null, new { @id = "btn0" })</li>
         <li>@Html.ActionLink("Manage Segment","Index","Segment", null, new { @id = "btn1" })</li>                             
         <li>@Html.ActionLink("Manage Module & URL", "Index", "Modules", null, new { @id = "btn2" })</li> 
         <li>@Html.ActionLink("Manage User Role", "Index", "UserRoles", null, new { @id = "btn3" })</li> 
 </ul>

Jquery的:

$(function () {
            $(*assign id here*).click(function () {
                $("#loading").fadeIn();
                var opts = {
                    lines: 10, // The number of lines to draw
                    length: 5, // The length of each line
                    width: 4, // The line thickness
                    radius: 10, // The radius of the inner circle
                    color: '#000', // #rgb or #rrggbb
                    speed: 1, // Rounds per second
                    trail: 60, // Afterglow percentage
                    shadow: false, // Whether to render a shadow
                    hwaccel: false // Whether to use hardware acceleration
                };
                var target = document.getElementById('loading');
                var spinner = new Spinner(opts).spin(target);
            });
        });

3 个答案:

答案 0 :(得分:1)

$(*assign id here*)更改为$('a[id^="btn"]')$('.dropdown-menu li a')

答案 1 :(得分:1)

$('.dropdown-menu li a').click(function(){
            $("#loading").fadeIn();
            var opts = {
                lines: 10, // The number of lines to draw
                length: 5, // The length of each line
                width: 4, // The line thickness
                radius: 10, // The radius of the inner circle
                color: '#000', // #rgb or #rrggbb
                speed: 1, // Rounds per second
                trail: 60, // Afterglow percentage
                shadow: false, // Whether to render a shadow
                hwaccel: false // Whether to use hardware acceleration
            };
            var target = document.getElementById('loading');
            var spinner = new Spinner(opts).spin(target);
        });

答案 2 :(得分:0)

您可以通过以下方式动态检查ID。 把它放在for循环或$ each func中 并添加以下

例如::

for(var i=0;i<= 5 ;i++)
{
   $('#' + 'btn' + i).Click()
       /*Do your function*/   

}
相关问题