【问题标题】:How could I use the html a tag to change div width and height?如何使用 html a 标签来更改 div 的宽度和高度?
【发布时间】:2014-03-04 22:37:00
【问题描述】:
<div class="panel">
    <a href onclick="manipulate()">Show Quick Admin (↑)</a>
    <div class="hidden">
        <ul>
            <li>
                <a href="admin.php">Panel</a>
            </li>
        </ul>
    </div>
</div>

所以我想为我正在制作的网站制作一个“快速管理栏”。这是我正在使用的代码,不要介意它不好的事实。我试图让它检查 div“面板”的高度和宽度,如果它是 20px,则更改“a”标签的 innerHTML,显示“隐藏”div,并使其在单击“a”时再次标记它会将 div 高度从 300px 设置回 20px,再次更改 a 标记的 innerHTML,并隐藏“隐藏”的 div。我该怎么做?

javascript:

function manipulate() {
    if ($("#panel").height() == "20") {
        document.getElementsByClassName("panel").style.height= "300px";
        document.getElementsByClassName("hidden").style = "";
    }
}

css:

.panel {
    padding:10px;
    background:#fffdbb;
    position:fixed;
    bottom:15px;
    width:225px; 
    height:20px; /* 300px */
    right:15px;
    border:10;
    border-style:solid;
    border-width:2px;
    border-color:red;
}

.hidden {
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
}

现在当单击显示快速管理时它什么也不做。我该如何解决这个问题?

【问题讨论】:

  • 这个$("#panel") 是 JQuery,它使用不存在的 id=panel 来寻址元素。您是否包含了 JQuery js 文件?尝试改用$("div.panel")
  • 另外,使用. 代替#

标签: javascript jquery css html


【解决方案1】:

你应该使用 [0]

 document.getElementsByClassName("panel")[0].style.height= "300px"

如果你使用jquery,为什么不使用这个?

$(".panel").css("height","300px");

我建议你使用 id 而不是 class

<div class="panel">

<div id="panel">

正确使用方法

http://jsfiddle.net/sb3fY/1/

    <div id="panel">
    <span class="link1">Show Quick Admin (↑)</span>
    <div class="hidden">
        <ul>
            <li>
               <a href="admin.php">Panel</a>
            </li>
        </ul>
    </div>
</div>

Javascript

$(".link1").on("click",function(){
  $("#panel").toggleClass("open");
  $("#hidden").toggle();
});

CSS

    #panel {
    padding:10px;
    background:#fffdbb;
    position:fixed;
    bottom:15px;
    width:225px; 
    height:20px; /* 300px */
    right:15px;
    border:10;
    border-style:solid;
    border-width:2px;
    border-color:red;
}

.hidden {
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
}

#panel.open {
 height: 300px;
}

【讨论】:

  • 谢谢。你认为我如何“隐藏和取消隐藏”div“隐藏”?
  • 哦哈哈,它在 Firefox 中不起作用,但在 chrome 中起作用。奇怪的!谢谢你的帮助 :) 它不允许我投票,因为我需要 15 个代表。
猜你喜欢
  • 2013-03-07
  • 2019-06-28
  • 1970-01-01
  • 2020-07-03
  • 2011-06-12
  • 1970-01-01
  • 2012-04-16
  • 2016-09-11
  • 2018-01-29
相关资源
最近更新 更多