简体   繁体   English

如何使用 jQuery 将 div css 的可见性更改为可见

[英]How to change visibility of a div css to visible with jQuery

I have NEXT and PREVIOUS buttons on my screen.我的屏幕上有 NEXT 和 PREVIOUS 按钮。 When the page initially loads I want the Previous button to be hidden and as soon as user clicks the Next button I want Previous button (div tag to be visible).当页面最初加载时,我希望隐藏上一个按钮,只要用户单击下一个按钮,我就希望上一个按钮(div 标签可见)。 I have a CSS property for Previous button where I am setting the visibility value to False.我有一个用于上一个按钮的 CSS 属性,我将可见性值设置为 False。 And also an if statement where I check to see if page counter is greater than 1 then change the visibility of navigationButtonTop to true.还有一个 if 语句,我检查页面计数器是否大于 1,然后将 navigationButtonTop 的可见性更改为 true。 It is not working..what am I doing wrong !?它不起作用..我做错了什么!?

 $(function() { $("#navigationButtonTop").css("visibility", "visible"); });
 div.navigationButtonTop { visibility: hidden; height: 100px; width: 100px; background-color:blue; }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="navigationButtonTop"></div>

firstly you have not closed your if statement and navigationButtonTop is a class not a id try this.首先你还没有关闭你的 if 语句和navigationButtonTop是一个类而不是一个 id 试试这个。

if (that.get("pageCounter") >= 1) {
     $(".navigationButtonTop").css("visibility", "visible");
}

as the OP has edited his question the new answer would be:由于 OP 编辑​​了他的问题,新答案将是:

$(function() {
  $(".navigationButtonTop").css("visibility", "visible");
});

First of all, the code of how you actually like to trigger the event, would be nice to know.首先,您实际上喜欢如何触发事件的代码很高兴知道。 Maybe the trigger is not working at all?也许触发器根本不起作用?

Additionaly you addressed differently.此外,您的处理方式不同。 In CSS navigationButtonTop is a class (see the ".") and in JS it's an id (see the "#").在 CSS navigationButtonTop是一个类(见“.”),而在 JS 中它是一个 id(见“#”)。 Is this the culprit in your atual code?这是你 atual 代码的罪魁祸首吗? If not I'll assume it's an id further...如果不是,我会进一步假设它是一个 id ......

For more readability try to move your visibility: hidden to an extra hidden class.为了提高可读性,尝试移动你的visibility: hidden到一个额外的隐藏类。 So you just can trigger:所以你可以触发:

$("#navigationButtonBottom").on('click', function() {
  $("#navigationButtonTop").removeClass('hidden');
});

And in you HTML add the class hidden to your button并在您的 HTML 中添加hidden到您的按钮的类

#navigationButtonTop.hidden { visibility:hidden; }

Or do it using javascript:或者使用javascript来做:

jQuery(document).ready( function($) {
  $("#navigationButtonTop").addClass('hidden');
})

In your JS you use ID ("#" sign before selector's name).在您的 JS 中,您使用 ID(选择器名称前的“#”符号)。 But in your CSS you use CLASS (dot sign before selector's name).但是在您的 CSS 中,您使用 CLASS(选择器名称前的点号)。

Try to use "#" in both cases (or dot accordingly).尝试在两种情况下都使用“#”(或相应的点)。

你试过 $("#navigationButtonTop").show();

Here a good jQuery plugin that saved me in a Materialize's collapsible customization, to avoid the use of a click event replaced with this visibilityChanged event. 这里有一个很好的jQuery 插件,它使我节省了Materialise 的可折叠自定义,以避免使用被此visibilityChanged事件替换的click事件。

In particular, the used code (among the various alternatives described in the plugin page) has been:特别是,使用的代码(在插件页面中描述的各种替代方案中)是:

HTML HTML

<li id="collapsible_trigger">
    <div class="collapsible-header"></div>
    <div class="collapsible-body"></div>
</li>

<script type="text/javascript" charset="utf-8" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" charset="utf-8" src="hideshow.min.js"></script>

JS JS

$("#collapsible_trigger .collapsible-body").hideShow();
$("#collapsible_trigger .collapsible-body").on("visibilityChanged", function(event, visibility){
    /* your custom code here */
});

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

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