切换时超链接未打开
我已经尝试解决这个问题好几天了,但我被困住了。我想要做的是:
当我进入页面时,我有几个链接,单击这些链接时应该打开一个链接,同时折叠并显示容器中的文本。单击链接时,图像会发生变化。我唯一无法开始工作的是打开链接,它没有做任何事情。
如果有人能帮助我,我会很棒/谢谢
<script>
$(document).ready(function(){
$(".toggle_container").hide();
$("p.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
return false;
});
});
</script>
<style type="text/css">
p.trigger {
padding: 0 0 0 10px;
margin: 0 0 5px 0;
background: url(image/arrow.gif) no-repeat;
height: 21px;
line-height: 21px;
width: 230px;
float: left;
}
p.trigger a {
color: #fff;
text-decoration: none;
display: block;
}
p.trigger a:hover { color: #ccc; }
p.active {background-position: left bottom;}
.toggle_container {
margin: 0 0 5px;
padding: 0;
overflow: hidden;
font-size: 1.2em;
width: 230px;
clear: both;
}
.toggle_container .block {
padding: 0 0 0 10px;
margin: 0 0 5px 0;
font-size:11px;
}
</style>
<h2>Header</h2>
<p class="trigger"><a style="color:#000;" href="http://www.loremipsum.com?id=er">» link</a></p>
<div class="toggle_container">
<div class="block">
» text
</div>
</div>
I've been trying to solve this problem now for days, but I am stuck. What I want to do is the following:
When I enter the page I have a couple of links that when clicked should open a link and at the same time colapse and show the text in the container. When the link is clicked an image is changed. The only thing I cannot get to work is the opening of the link, it does not do anything.
I would be fantastic if someone could help me/thanx
<script>
$(document).ready(function(){
$(".toggle_container").hide();
$("p.trigger").click(function(){
$(this).toggleClass("active").next().slideToggle("slow");
return false;
});
});
</script>
<style type="text/css">
p.trigger {
padding: 0 0 0 10px;
margin: 0 0 5px 0;
background: url(image/arrow.gif) no-repeat;
height: 21px;
line-height: 21px;
width: 230px;
float: left;
}
p.trigger a {
color: #fff;
text-decoration: none;
display: block;
}
p.trigger a:hover { color: #ccc; }
p.active {background-position: left bottom;}
.toggle_container {
margin: 0 0 5px;
padding: 0;
overflow: hidden;
font-size: 1.2em;
width: 230px;
clear: both;
}
.toggle_container .block {
padding: 0 0 0 10px;
margin: 0 0 5px 0;
font-size:11px;
}
</style>
<h2>Header</h2>
<p class="trigger"><a style="color:#000;" href="http://www.loremipsum.com?id=er">» link</a></p>
<div class="toggle_container">
<div class="block">
» text
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的链接无法“到达任何地方”的原因是您在
onClick
事件中返回 false
。您告诉链接不遵循其默认操作,因此它不会遵循该链接。但是,如果您删除
return false
,那么每当您单击该链接时,您的页面都会重定向。我的问题是,您要更改图像还是要在新窗口中打开链接?或者您只想将内容加载到页面中?
假设您想在单击链接时更改图像,您需要做的就是更改 IMG 的 SRC。
HTML
JAVASCRIPT
如果您想在新窗口中打开链接,则只需更改您的 HTML:
HTML
在 Javascript 中,只需删除 < code>return false 以遵循默认链接行为。
JAVASCRIPT
如果您想将内容加载到页面中:
HTML
JAVASCRIPT
The reason why your link is not "going anywhere" is because on your
onClick
event you havereturn false
. You're telling the link to not follow it's default action, therefore it will not follow the link.However, if you remove the
return false
, then whenever you click on the link, your page will just redirect.My question is, do you want to change an image or do you want to open the link in a new window? Or do you want to just load the content into the page?
Assuming that you want to change the image when the link is clicked what you need to do is to change the SRC of your IMG.
HTML
JAVASCRIPT
If you want to open the link in a new window, then just change your HTML:
HTML
In your Javascript, just remove your
return false
to follow the default link behavior.JAVASCRIPT
And if you want to load the content into the page:
HTML
JAVASCRIPT
您想在动画完成后打开 href 吗?如果是这样,您可以使用
slideToggle()
上的回调来触发window.location
事件。Do you want to open the href after the animation has complete? If so, you could use the callback on
slideToggle()
to fire awindow.location
event.