javascript - 无法创建动态 div 以在单击和返回时更改大小

标签 javascript jquery html css

我创建了一个包含多个 div 的动态页面,每个 div 的大小设置为 30%,现在我尝试制作一个 Jquery,允许您在每次单击时将 div 大小更改为 100% 并返回 30%在重新点击 div 时,不幸的是我的代码不起作用:

这是我当前使用的 jquery:

$( document ).ready(function() {
  $(".div").click(function() {
    if($(this).css("width") == "30%")
      $(this).css("width", "100%");
    else
      $(this).css("width", "30%");
  }); 
});

那是div的css

.div{
    min-height:430px;
    box-shadow: 1px 1px 7px 0 rgba(0,0,0,0.1);
    margin:10px;
    float:left;
    width: 30%;
    min-width: 300px;
    transition: width 1s;
}

有什么解决办法吗?

最佳答案

将其与父宽度进行比较:

$( document ).ready(function() {
  $(".div").click(function() {
    if($(this).outerWidth()==$(this).parent().width())
      $(this).css("width", "30%");
    else
      $(this).css("width", "100%");
  }); 
});

关于javascript - 无法创建动态 div 以在单击和返回时更改大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37479627/

相关文章:

javascript - 如何手动启动 Bootstrap Tour?

javascript - 使用完整路径获取文件对象

javascript - 使用JQuery获取不同行上两个表格单元格之间的所有表格单元格

javascript - 为什么这个 jquery each() 循环不对唯一类进行操作

javascript - 在垂直高度的 50% 处停止使用 div 进行固定位置滚动?

python - 在多页表中查找页数

javascript - 找出我刚刚点击了哪个子元素

javascript - 使用 React 增加数字直到达到特定数字

javascript - 带有 location.href 和 document.referrer 的 Html 按钮

html - <select> 和 :after with CSS in WebKit 的问题