简体   繁体   English

jQuery 隐藏仅在切换全屏时有效

[英]jQuery hide only works when toggling fullscreen

I want to toggle hiding "myDIV" with jQuery.我想用 jQuery 切换隐藏“myDIV”。 Works perfect on mobile, but when pressing the button on desktop only works if you toggle fullscreen after presing the button, otherwhise it doesn't.在移动设备上完美运行,但在桌面上按下按钮时,只有在按下按钮后切换全屏时才有效,否则无效。 Anyone had this problem before?以前有人遇到过这个问题吗?

Heres a link to the site:这是该网站的链接:

https://mkt.partners/analytical/draft.html https://mkt.partners/analytical/draft.html

the button is on line 616按钮在第 616 行

<a onclick="javascript:toggler('myDIV');" class="btn btn-primary lookbook-action"><b>Ver más.</b></a>

the div im trying to hide is in line 618我试图隐藏的 div 在第 618 行

<div id="myDIV" class="hidden">

CSS class "hidden" just hides the div CSS class “隐藏”只是隐藏了 div

    <style type="text/css">
        .hidden {
     display:none;
}
    </style>

and my script is in line 1460我的脚本在第 1460 行

    <script>
function toggler(divId) {
  $("#" + divId).toggle("slow");
}

</script>

From JQuery's documenation:来自 JQuery 的文档:

With no parameters, the .toggle() method simply toggles the visibility of elements [...] The matched elements will be revealed or hidden immediately, with no animation, by changing the CSS display property.在没有参数的情况下, .toggle()方法只是简单地切换元素的可见性 [...] 通过更改 CSS 显示属性,匹配的元素将立即显示或隐藏,没有 animation。

This means that JQuery is directly modifying the inline style attribute.这意味着 JQuery 是直接修改内联样式属性。

Your problem comes from the fact that you have this item's display property set to none in another location (your CSS).您的问题来自这样一个事实,即您在其他位置(您的 CSS)将此项目的display属性设置为none So, your button is hidden 100% of the time, regardless of what JQuery does.因此,无论 JQuery 做什么,您的按钮 100% 的时间都是隐藏的。

It's like you've got something covered with a curtain, and then try and hide it again by covering it with another curtain... only to pull back the second one and wonder why your object isn't visible — it's still covered by the first curtain!就像您用窗帘盖住了一些东西,然后尝试用另一个窗帘盖住它再次隐藏它......只是拉回第二个窗帘并想知道为什么您的 object 不可见 - 它仍然被第一幕!


Get rid of the extra CSS (the hidden class) and let JQuery deal with the hiding.去掉多余的 CSS( hidden类),让 JQuery 处理隐藏。

 function toggler(divId) { $("#" + divId).toggle("slow"); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="myDIV" style="background-color: red; width: 5em; height: 5em;"></div> <,-- Use a button, not an anchor; since this isn't a link --> <button onclick="toggler('myDIV')," class="btn btn-primary lookbook-action"> <.-- <b> tags are deprecated, use strong instead --> <strong>Ver más.</strong> </button>

The whole "this works on desktop but only if you fullscreen, and it always works on mobile" thing makes me think that you've got this CSS mishap behind some media queries or something.整个“这适用于桌面,但只有当你全屏时,它总是适用于移动设备”这件事让我觉得你在一些媒体查询或其他东西背后有这个 CSS 事故。

I tested the live link you gave, simply turning off display: none in my DevTools in the .hidden class fixed the issue, and the Ver más button successfully hides the div .我测试了您提供的实时链接,只需关闭display: none在我的 DevTools 中的.hidden class 修复了问题,并且Ver más按钮成功隐藏了div

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM