javascript - DIV 的显示和隐藏选项(需要修改)

标签 javascript html css

JavaScript - 使用链接和 CSS 显示或隐藏 div

这里是用java脚本显示和隐藏DIV

代码

<a onclick="document.getElementById('div_name2').style.display = '';
            return false;" 
       href="" style="text-decoration:none;border-bottom:1px dotted blue;">
        Show more...</a>
    <br />
    <div id="div_name2" style="display:none;margin:15px 15px 0px 15px;padding:5px;border:1px solid #aaa;">
        This is more information placed in our hidden div.  
        When you click on the "hide" link this DIV area will disappear:  
        <a onclick="document.getElementById('div_name2').style.display = 'none';
                return false;" href="" 
           style="text-decoration:none;border-bottom:1px dotted blue;">hide</a>
    </div>

这是我正在使用的代码

  • 现在我需要的是当页面加载时我需要DIV可见
  • 如何在加载页面时使 DIV 可见

错误

当我加载这个页面时,div 是隐藏的我如何让 div 可见???

最佳答案

试试这段代码:

<a onclick="document.getElementById('div_name2').style.display = 'block';
            return false;" 
       href="" style="text-decoration:none;border-bottom:1px dotted blue;">
        Show more...</a>
    <br />
    <div id="div_name2" style="display:block;margin:15px 15px 0px 15px;padding:5px;border:1px solid #aaa;">
        This is more information placed in our hidden div.  
        When you click on the "hide" link this DIV area will disappear:  
        <a onclick="document.getElementById('div_name2').style.display = 'none';
                return false;" href="" 
           style="text-decoration:none;border-bottom:1px dotted blue;">hide</a>
    </div>

你应该给你的 div 属性“display:block”,以便在网站加载时显示它。现在,当您点击您的链接时,您应该将 display 设置为 none 以隐藏 div_name2

关于javascript - DIV 的显示和隐藏选项(需要修改),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26321212/

相关文章:

javascript - 如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?

javascript - jQuery 中的 getJSON 长度

javascript - 焦点在 IE 中不起作用

html - 填充不适用于我的导航栏

javascript - 如何制作像这种结构的内容 slider

javascript - jquery - 按类别识别项目,然后提取值

javascript - 在 jQuery 中循环遍历具有相同类的 div

javascript - vuejs 动态使用 v-model

javascript - Disable vs readonly 不适用于非交互式但值保持器输入

php - 显示来自 TABLE 的具有不同大小的个体的图像