[英]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();
}
});
});
});
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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.