如何移除溢出:auto属性



我已经为div设置了overflow属性为auto .但是当我点击按钮时,我希望div应该展开并希望overflow属性被删除。

#viewNotings{
  width: 310px;
  overflow-x: auto;
}

我想在另一个页面中删除这个溢出属性。

如果你想要一个点击时的行为,在元素(div, span, img, a…)的"onclick"字段中声明。

如果你想要这个行为来修改一个样式属性,请相应地设置this.style. property。

->

<div onclick="this.style.overflow='visible';">

正确的代码是

document.getElementById("viewNotings").style.cssText="overflow-x:visible;"; 
https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/cssText

cssText只返回或设置元素的内联样式声明的文本。

答案很简单,为overflow属性设置一个空值就足够了。

<div onclick="this.style.overflow=' ';">
  • 为div
  • 注册jquery点击事件
  • 点击,移除溢出并设置宽度为100%

$(document).ready(function() {
  $("#viewNotings").click(function () {
    $("#viewNotings").css("overflow", "none");
    $("#viewNotings").css("width", "100%");
  });
 });
#viewNotings{
  width: 310px;
  background: gray;
  height: 30px;
  overflow-x: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <div id="viewNotings"></div>
</body>

最新更新