最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I convert this working JAVASCRIPT code to JQUERY - Stack Overflow

programmeradmin0浏览0评论
 <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>
 <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>
Share Improve this question asked May 22, 2009 at 18:55 ShivaShiva 1,4092 gold badges16 silver badges32 bronze badges 6
  • 1 Given that jQuery is JavaScript, I'm having trouble not making the glib response and reposting your exact code as an answer. :P – Dan Lew Commented May 22, 2009 at 18:57
  • Why do you need to do this? What jquery features do you need to use? What is your objective? ETC? – jrcs3 Commented May 22, 2009 at 18:59
  • 3 @Daniel: while all jQuery is Javascript, not all Javascript is jQuery. – tvanfosson Commented May 22, 2009 at 19:01
  • 1 I am just curious how elegent and pact code might be with JQUERY. Of course, this code is working fine --- does what it needs to be done; of course I have tested this code on any other browser beside IE . Thanks. – Shiva Commented May 22, 2009 at 19:01
  • @Daniel - no offense intended. – tvanfosson Commented May 22, 2009 at 19:08
 |  Show 1 more ment

6 Answers 6

Reset to default 10

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

$(function() {
   var changed = <%= HasPasswordAlreadyChanged %>;
   if (changed) {
      $('#sNav a').attr('disabled','disabled')
                  .click( function() { return false; } );
   }
});
<script language="javascript" type="text/javascript">
  $(function(){
    if ('<%  = HasPasswordAlreadyChanged %>' == '1') {
      $("#sNav").find("a").attr("disabled","disabled").click(function(){return false;});
    }
  });
</script>
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; }) : ""; 
});

Presuming HasPasswordAlreadyChanged is either 0 or 1 (or 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 ?

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.

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.

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

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

function hasPasswordChanged() {
    // unchanged
}

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

评论列表(0)

  1. 暂无评论