为jquery分配动态id

assign dynamic id to jquery

本文关键字:id 动态 分配 jquery      更新时间:2023-09-26

我有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);
            });
        });

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

$('.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);
        });

您可以通过以下方式动态检查id。将其放入for循环或$each函数中并添加以下

例如:

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