如何将此工作JAVASCRIPT代码转换为JQUERY

时间:2022-03-09 16:56:41
 <script language="javascript" type="text/javascript">


function hasPasswordChanged(value)
  {
        if(value == '1')
        {
            var container = document.getElementById("sNav");
            if(document.getElementsByTagName)
            {
               var hyperLinkList = container.getElementsByTagName("a");

               for(var currentLink in hyperLinkList)
               {
                    hyperLinkList[currentLink].disabled = true;
                    hyperLinkList[currentLink].onclick =function () { return false;}

               }

            }
        }
  }


  window.onload = function () 
  {
        hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
  }

</script>

6 个解决方案

#1


Assuming I'm correct in that you want to disable the nav links on the page if the password has already changed (1 is true).

假设我是正确的,如果密码已经改变(1为真),你想要禁用页面上的导航链接。

$(function() {
   var changed = <%= HasPasswordAlreadyChanged %>;
   if (changed) {
      $('#sNav a').attr('disabled','disabled')
                  .click( function() { return false; } );
   }
});

#2


<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>

#3


function hasPasswordChanged(value)
  {
        if(value == '1')
        {
            $('#sNav a').attr('disabled', 'true').click(function(){ return false; });
        }
  }

$(function(){
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
})

or a bit wierder:

或者有点啰嗦:

$(function(){
    <%  = HasPasswordAlreadyChanged %> == 1 ? $('#sNav a').attr('disabled', 'true').click(function(){ return false; }) : ""; 
});

#4


Presuming HasPasswordAlreadyChanged is either 0 or 1 (or flase/true)

假设HasPasswordAlreadyChanged为0或1(或flase / true)

jQuery(function($){
  !!<%= HasPasswordAlreadyChanged %> && $("#sNav a").attr("disabled",true).click(function(){return false;})
})

Also, does a disabled attribute on A element affect it in any way ?

此外,A元素上的禁用属性是否会以任何方式影响它?

#5


function hasPassWordChanged(value) {
  if (value == '1') {
    $("#sNav a").attr("disabled", true).click(function() {return false;});
  }
}

$(function() {
  hasPasswordChanged('<%  = HasPasswordAlreadyChanged %');
});

This selects all a tags that are children of the node with id sNav, sets all of their disabled attributes to true, and sets the specified returning false function to be called upon the click event.

这将选择具有id sNav的节点的子节点的所有标记,将其所有已禁用的属性设置为true,并设置要在click事件上调用的指定的返回false函数。

The last part, a call to $() with the specified function, runs the function when the DOM is ready to be worked on, and is a synonym for $(document).ready() when passed a function. You can also substitute this with your window.onload setting, but the call to $() is more preferred with jQuery.

最后一部分,使用指定函数调用$(),在DOM准备工作时运行该函数,并且在传递函数时是$(document).ready()的同义词。您也可以使用window.onload设置替换它,但使用jQuery更优先调用$()。

#6


As your JavaScript is working, there is probably no value in converting it to jQuery at the risk of introducing any bugs.

当你的JavaScript工作时,将它转换为jQuery可能没有任何价值,冒着引入任何错误的风险。

The only thing I might consider is using jQuery's event-handling rather than explicitly using window.onload :

我唯一可以考虑的是使用jQuery的事件处理而不是显式地使用window.onload:

function hasPasswordChanged() {
    // unchanged
}

$(document).ready(function() {
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
});

#1


Assuming I'm correct in that you want to disable the nav links on the page if the password has already changed (1 is true).

假设我是正确的,如果密码已经改变(1为真),你想要禁用页面上的导航链接。

$(function() {
   var changed = <%= HasPasswordAlreadyChanged %>;
   if (changed) {
      $('#sNav a').attr('disabled','disabled')
                  .click( function() { return false; } );
   }
});

#2


<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>

#3


function hasPasswordChanged(value)
  {
        if(value == '1')
        {
            $('#sNav a').attr('disabled', 'true').click(function(){ return false; });
        }
  }

$(function(){
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
})

or a bit wierder:

或者有点啰嗦:

$(function(){
    <%  = HasPasswordAlreadyChanged %> == 1 ? $('#sNav a').attr('disabled', 'true').click(function(){ return false; }) : ""; 
});

#4


Presuming HasPasswordAlreadyChanged is either 0 or 1 (or flase/true)

假设HasPasswordAlreadyChanged为0或1(或flase / true)

jQuery(function($){
  !!<%= HasPasswordAlreadyChanged %> && $("#sNav a").attr("disabled",true).click(function(){return false;})
})

Also, does a disabled attribute on A element affect it in any way ?

此外,A元素上的禁用属性是否会以任何方式影响它?

#5


function hasPassWordChanged(value) {
  if (value == '1') {
    $("#sNav a").attr("disabled", true).click(function() {return false;});
  }
}

$(function() {
  hasPasswordChanged('<%  = HasPasswordAlreadyChanged %');
});

This selects all a tags that are children of the node with id sNav, sets all of their disabled attributes to true, and sets the specified returning false function to be called upon the click event.

这将选择具有id sNav的节点的子节点的所有标记,将其所有已禁用的属性设置为true,并设置要在click事件上调用的指定的返回false函数。

The last part, a call to $() with the specified function, runs the function when the DOM is ready to be worked on, and is a synonym for $(document).ready() when passed a function. You can also substitute this with your window.onload setting, but the call to $() is more preferred with jQuery.

最后一部分,使用指定函数调用$(),在DOM准备工作时运行该函数,并且在传递函数时是$(document).ready()的同义词。您也可以使用window.onload设置替换它,但使用jQuery更优先调用$()。

#6


As your JavaScript is working, there is probably no value in converting it to jQuery at the risk of introducing any bugs.

当你的JavaScript工作时,将它转换为jQuery可能没有任何价值,冒着引入任何错误的风险。

The only thing I might consider is using jQuery's event-handling rather than explicitly using window.onload :

我唯一可以考虑的是使用jQuery的事件处理而不是显式地使用window.onload:

function hasPasswordChanged() {
    // unchanged
}

$(document).ready(function() {
    hasPasswordChanged('<%  = HasPasswordAlreadyChanged %>');
});