简体   繁体   English

Javascript链接停止工作,无法弄清原因

[英]Javascript links stopped working, can't figure out why

I have a website that is using js to switch between two different navigation menus. 我有一个使用js在两个不同的导航菜单之间切换的网站。 It has always worked up until a week ago or so and I can't figure out why. 它一直工作到一个星期前左右,我不知道为什么。 Wondering if anyone here can point something out to me that I'm not seeing. 想知道这里是否有人可以向我指出我没有看到的东西。

Here's my markup: 这是我的标记:

<div class="catMenu">
    <a href="javascript:showonlyone('newboxes1');">Works</a>
</div>

<div class="newboxes" id="newboxes1" style="display:block;" >
    ...the 1st menu
</div>

<div class="catMenu2">
    <a href="javascript:showonlyone('newboxes2');" >About</a>
</div>

<div class="newboxes" id="newboxes2"  style="display:none;">
    ...the 2nd menu
</div> 

And Here's my js: 这是我的js:

$(document).ready(function() {
    function showonlyone(thechosenone) {
       $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show();
          }
          else {
               $(this).hide();
          }
        });
    }
});

When I look at my console using firebug, I get the following error: 使用firebug查看控制台时,出现以下错误:

ReferenceError: showonlyone is not defined

but with my lack of knowledge about js, I'm not entirely sure how to fix this. 但是由于缺乏对js的了解,我不确定如何解决此问题。

Can anyone shed some light? 谁能阐明一些想法?

If you're going to invoke the showonlyone function using href="javascript:showonlyone()" you'll have to define it in the global scope, currently it's defined inside your document.ready() function. 如果要使用href="javascript:showonlyone()"调用showonlyone函数,则必须在全局范围内对其进行定义,当前它是在document.ready()函数中定义的。

You can also expose it explicitly using the window keyword: 您还可以使用window关键字显式公开它:

$(document).ready(function() {
    window.showonlyone = function showonlyone(thechosenone) {
       $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show();
          }
          else {
               $(this).hide();
          }
        });
    }
});

Building on haim770's answer 以haim770的答案为基础

function showonlyone(thechosenone) {
   $('.newboxes').each(function(index) {
      if ($(this).attr("id") == thechosenone) {
           $(this).show();
      }
      else {
           $(this).hide();
      }
    });
}

$(document).ready(function() {});

Try this: 尝试这个:

$(document).ready(function() {});
    function showonlyone(thechosenone) {
       $('.newboxes').each(function(index) {
          if ($(this).attr("id") == thechosenone) {
               $(this).show();
          }
          else {
               $(this).hide();
          }
        });
    }

You're using jQuery, so move your inline JavaScript out of your HTML and use a data attribute to store thechosenone variable. 您正在使用jQuery,因此将内联JavaScript移出HTML,并使用data属性存储thechosenone变量。 Then you can use jQuery to add click methods to your anchors, grab the variable from the data attribute and run the rest of the code. 然后,您可以使用jQuery将click方法添加到锚点,从data属性中获取变量,然后运行其余代码。

HTML 的HTML

<div class="catMenu">
  <a data-chosen="newboxes1">Works</a>
</div>

JS JS

$(document).ready(function() {
  $('a').click(function () {
    var thechosenone = $(this).data('chosen');
    $('.newboxes').each(function(index) {
      if ($(this).attr("id") == thechosenone) {
        $(this).show();
      } else {
        $(this).hide();
      }
    });        
  });
});

DEMO 演示

While your format is close to working and haim770 has already shown the problem, I would change your format a bit, here. 虽然您的格式即将运作并且haim770已经显示出了问题,但是我会在这里稍作更改。 Try this: 尝试这个:

<div class="catMenu">
    <a id="LinkNewBoxes1" class="ToggleDisplay">Works</a>
</div>

<div class="newboxes" id="NewBoxes1" style="display:block;" >
    ...the 1st menu
</div>

<div class="catMenu2">
    <a id="LinkNewBoxes2" class="ToggleDisplay" >About</a>
</div>

<div class="newboxes" id="NewBoxes2"  style="display:none;">
    ...the 2nd menu
</div> 


$(document).ready(function() {
    $(".ToggleDisplay").click(function(e){ //Event Handler for ToggleDisplay clicks
        var BoxToDisplay = $(this).prop("id").replace("Link", ""); //Removes 'Link' from anchor tags ID and assigns to variable
        $(".newBoxes").each(function(){
            if ($(this).prop("id") == BoxToDisplay) //Check if this is the right one to display
                $(this).show();
            else
                $(this).hide();
        });
       e.preventDefault(); //Prevent browser's default action for anchor clicks
       return false;
    });
});

Thanks Andrew! 谢谢安德鲁!

HTML 的HTML

<div class="catMenu">
    <a href='#' id="LinkNewBoxes1" class="ToggleDisplay">Works</a>
</div>
<div class="newboxes" id="NewBoxes1" style="display:block;" >
    ...the 1st menu
</div>
<div class="catMenu2">
    <a href='#' id="LinkNewBoxes2" class="ToggleDisplay" >About</a>
</div>    
<div class="newboxes" id="NewBoxes2"  style="display:none;">
    ...the 2nd menu
</div>  

JS JS

$(".ToggleDisplay").click(function(e){ 
    var BoxToDisplay = $(this).prop("id").replace("Link", ""); 
    $(".newboxes").each(function(){
        //Check if this is the right one to display
        if ($(this).prop("id") == BoxToDisplay) 
            $(this).show();
        else
            $(this).hide();
    });
   e.preventDefault(); 
   return false;
});

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的 javascript 验证刚刚停止工作,我不知道为什么 - My javascript validation just stopped working and I can't figure out why addEventListener()不起作用-无法找出原因 - addEventListener() Not Working - can't figure out why 无法弄清楚为什么循环不起作用 - Can't figure out why the loop is not working 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 无法弄清楚为什么它不起作用。 javascript调用数据键 - Can't figure out why it isn't working. javascript calling data-key 似乎无法弄清楚为什么我的JavaScript无法在IE和Chrome中运行 - Can't seem to figure out why my javascript isn't working in IE and Chrome 无法弄清楚为什么javascript无法在我的php文件中工作 - Can't figure out why javascript isn't working in my php file 编码新手,我不明白为什么我的 javascript 按钮无法正常工作? - New to coding,I can't figure out why my javascript buttons are not working properly? 无法弄清楚为什么javascript会在JavaScript中评估为unidentified - Can't figure out why javascript evaluates to unidentified in JavaScript 按键上的Javascript:player.style.left + = 10,无法弄清楚为什么这不起作用 - Javascript on keypress: player.style.left += 10, can't figure out why this is not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM