嗨,朋友我有div问题。 我有一个链接显示/隐藏在我的网页上点击,我必须显示或隐藏特定的div。 我做得很成功。 但我的问题是,只要我点击该链接div被隐藏或显示,但页面直接在顶部&我必须再次滚动下来。 我不想滚动这个,也不想跳到顶部。HTML Divs显示/隐藏问题
请帮我解决这个问题。 预先感谢您。
更新: 朋友我从我的一个朋友那里得到了答案。 其实我正在使用 由于href =“#”每当我点击该链接时,网址都会被更改并且页面会显示在顶部。
嗨,朋友我有div问题。 我有一个链接显示/隐藏在我的网页上点击,我必须显示或隐藏特定的div。 我做得很成功。 但我的问题是,只要我点击该链接div被隐藏或显示,但页面直接在顶部&我必须再次滚动下来。 我不想滚动这个,也不想跳到顶部。HTML Divs显示/隐藏问题
请帮我解决这个问题。 预先感谢您。
更新: 朋友我从我的一个朋友那里得到了答案。 其实我正在使用 由于href =“#”每当我点击该链接时,网址都会被更改并且页面会显示在顶部。
您是否正在尝试这么做?
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<div id="wrapper">
<div id="container">
</div><!-- end of #container div -->
<a id="showdiv">Show the div</a>|<a id="hideDiv">Hide the div</a>|<a id="toggle">Toggle</a>
</div><!-- end of #wrapper div -->
</body>
</html>
这里的CSS:
#container {
width: 300px;
height: 300px;
background: red;
margin-bottom: 20px;
}
#wrapper {
margin: 40px auto;
width: 400px;
}
而这里的jQuery的
$(function() {// When document is ready, run this...
//Get hold of the link with the id #showdiv and do something when you click it
$("#showdiv").click(function() {
// Grab the div with the id #container and show it
// Alert me when you're done
$("#container").show(2000, function() {
alert("I'm done showing");
});
});
//Get hold of the link with the id #hideDiv and do something when you click it
$("#hideDiv").click(function() {
// Grab the div with the id #container and hide it
// Alert me when you're done
$("#container").hide(2000, function() {
alert("I'm done hiding");
});
});
// Toggle - This is like a On/Off Switch
//Get hold of the link with the id #toggle and do something when you click it
$("#toggle").click(function() {
// Grab the div with the id #container and show if hidden/hide if shown
$("#container").toggle(2000);
});
});
当然你必须使用上面的脚本之前链接到jQuery的副本。 以下是演示的链接:http://jsfiddle.net/tonystark/HhNBA/
这是因为你的链接指向类似#foo
或只是#
,而它不应该有一个href
(或有一个空的)...
你probaby有像href=#
href属性一个#请删除哈希和替代那种编写href='javascript:void(null);'
假设你有一个链接
内联(不推荐,但可能是你有什么)
<script>
function showhide(id) {
var elem = document.getElementById(id);
elem.style.display=elem.style.display=="none"?"block":"none";
return false; // MANDATORY
}
</script>
<a href="#" onclick="return showhide('someId')">Toggle</a>
<div id="someId">Show or hide me when you click a link</div>
取出href属性并应用文字装修风格
<a style="text-decoration: underline;" title="click me"></a>
那会显得更有意义不是将你不想一个href,后来阻止其正常运行。一个优雅退化的替代方法是使用href指向显示/隐藏的div,默认显示正常,让javascript将其隐藏在可用的javascript的位置。
“改变你的锚(链接)到一个跨度,以便它没有这种行为,然后,使用css风格,所以它看起来你希望它看起来。”
我经常使用该技术。即使用带有onclick的span或div,使用文本修饰:下划线,光标:指针和可能的display:inline,如果您决定使用div。一个需要注意的是,在IE6中,css hover不能用于除锚点之外的任何其他功能。这很容易与JavaScript解决。
你完全可以删除href属性。锚具有上述优点(跨浏览器:悬停样式)并允许用于工具提示等的标题属性。
这甚至不显示... – mplungjan 2011-12-30 13:14:27
您必须取消链接的onclick
处理程序的默认行为。这样做,不要在您单击处理程序使用return false
,而是使用event.preventDefault()
:
HTML:
<a href="#targetdiv" class="foo">hide me</a>
<div id="#targetdiv">blah</div>
的Javascript:
document.querySelector('a.foo').onclick = function(event) {
try {
document.querySelector(this.getAttribute('href')).style.display = 'none';
} catch (e) {
console.error("couldn't find element to hide");
}
event.preventDefault();
}
JQuery的:
$('a.foo').click(function(event) {
try {
$($(this).attr('href')).hide();
} catch (e) {
console.error("couldn't find element to hide");
}
event.preventDefault();
})
更多信息:
相当新的选择器有趣的介绍。 [兼容性](https://developer.mozilla.org/En/DOM/Document.querySelector#section_5) – mplungjan 2011-12-30 09:12:08
您可以发布您的示例代码或http://jsfiddle.net – Pavan 2011-12-30 06:18:14
创建的jsfiddle请分享您的代码, – 2011-12-30 06:31:24