a标签 href onclick调用函数的问题

yangzj1992 yangzj1992 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
body
{
margin: 0 auto;
padding: 0;
}
a:focus
{
outline: none;
}
#pn
{
background: #e8e8e8;
width: 600px;
display: block;
margin: 0 auto;
padding: 5px;
font-size: 9pt;
height: auto;
}
.slide
{
margin: 0;
padding: 0;
width: 600px;
border-top: solid 4px gray;
margin: 0 auto;
}
.btn-slide
{
background: gray;
text-align: center;
width: 120px;
height: 30px;
padding: 10px 10px 0 0;
margin: 0 auto;
display: block;
color: #fff;
text-decoration: none;
}
</style>
<script type="text/javascript">
function showdiv() {
document.getElementById("hpn").style.display = "block";
// document.getElementById("pn").style.height='80px';
document.getElementById("strHref").href = "javascript:hidediv();";
document.getElementById("strHref").innerHTML = "收起-";
}
function hidediv() {
document.getElementById("hpn").style.display = "none";
// document.getElementById("pn").style.height='60px';
document.getElementById("strHref").href = "javascript:showdiv();";
document.getElementById("strHref").innerHTML = "更多选项+";
}
</script>
</head>
<body>
<div>
<div id="pn">
<p>
手机 - 商品筛选</p>
<p>
网络: 移动4G(TD-LTE ) 联通4G(TD-LTE) 联通3G(WCDMA)
</p>
<div id="hpn" style="display: none">
<p>
价格: 5000以上 4000-4999 3000-3999 2000-2999
</p>
<p>
特点: JDPhone计划 "0"元购机 防水 长待机 1080P全高清屏
</p>
</div>
</div>
<p class="slide">
<a href="javascript:showdiv();" id="strHref" class="btn-slide">更多选项+</a></p>
</div>
</body>
</html>

上面这样调用可以执行showdiv()

但是用onclick如下就不能执行,这是为什么呢?求解.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
body
{
margin: 0 auto;
padding: 0;
}
a:focus
{
outline: none;
}
#pn
{
background: #e8e8e8;
width: 600px;
display: block;
margin: 0 auto;
padding: 5px;
font-size: 9pt;
height: auto;
}
.slide
{
margin: 0;
padding: 0;
width: 600px;
border-top: solid 4px gray;
margin: 0 auto;
}
.btn-slide
{
background: gray;
text-align: center;
width: 120px;
height: 30px;
padding: 10px 10px 0 0;
margin: 0 auto;
display: block;
color: #fff;
text-decoration: none;
}
</style>
<script type="text/javascript">
function showdiv() {
document.getElementById("hpn").style.display = "block";
// document.getElementById("pn").style.height='80px';
document.getElementById("strHref").href = "javascript:hidediv();";
document.getElementById("strHref").innerHTML = "收起-";
}
function hidediv() {
document.getElementById("hpn").style.display = "none";
// document.getElementById("pn").style.height='60px';
document.getElementById("strHref").href = "javascript:showdiv();";
document.getElementById("strHref").innerHTML = "更多选项+";
}
</script>
</head>
<body>
<div>
<div id="pn">
<p>
手机 - 商品筛选</p>
<p>
网络: 移动4G(TD-LTE ) 联通4G(TD-LTE) 联通3G(WCDMA)
</p>
<div id="hpn" style="display: none">
<p>
价格: 5000以上 4000-4999 3000-3999 2000-2999
</p>
<p>
特点: JDPhone计划 "0"元购机 防水 长待机 1080P全高清屏
</p>
</div>
</div>
<p class="slide">
<a href="#" id="strHref" class="btn-slide" onclick="showdiv()">更多选项+</a></p>
</div>
</body>
</html>
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
yangzj1992
CSS 61,117 成员 60,701 内容