简体   繁体   English

在Jquery中隐藏和更改div元素

[英]Hiding and changing div elements in Jquery

Hi Guys i kindly need your help,i have two buttons on web page when button1 is clicked an arrow appears underneath it and the button's background color changes,when button2 is clicked same applies it but the arrow underneath button1 disappears and the background color changes. 嗨,大家好,我需要您的帮助,单击按钮1时,我在网页上有两个按钮,箭头下方出现了一个按钮,并且按钮的背景颜色发生了变化,单击按钮2时也应用了一个按钮,但是按钮1下方的箭头消失了,并且背景颜色发生了变化。

I have implemented most of these properties but i can't figure out why after clicking on button2 the going back to button1 the arrow underneath button2 does not hide. 我已经实现了其中的大多数属性,但是我无法弄清楚为什么在单击button2之后返回button1时button2下面的箭头没有隐藏。

here is my code 这是我的代码

++++++++ HTML +++++++ ++++++++ HTML +++++++

  $(document).ready(function() { $('#select').click(function(){ $(this).css('background','#1169ff'); $(this).css('color','#ecf0f1'); $('#triangle_down').add('#triangle_down1').show(); if($('#select1').css('background','#1169ff') && $('#select1').css('color','#ecf0f1')){ $('#select1').css('background','#ecf0f1'); $('#select1').css('color','#1169ff'); } else if($('#triangle_down2').add('#triangle_down3').addClass('hidden')){ $('#triangle_down').add('#triangle_down1').show(); } }); $('#select1').on('click', function() { $(this).css('background','#1169ff'); $(this).css('color','#ecf0f1'); $('#triangle_down').add('#triangle_down1').addClass('hidden'); $('#select').css('background','#ecf0f1'); $('#select').css('color','#1169ff'); $('#triangle_down2').add('#triangle_down3').show(); }); }) 
 #subscribe p{ text-align:center; } #subscribe input { background: rgba(255, 255, 255, 0.52); color: #666666; vertical-align: middle; width: 593px; border: 1px solid rgba(255, 255, 255, 0.76); padding: 0 10px; height: 60px; font-size: 20px; outline: 0; } #subscribe input[type="submit"]{ background: rgb(255, 135, 19); color: #ecf0f1; /*width: auto;*/ padding: 14px 25px; cursor: pointer; margin-left: 50px font-weight: bold; height: 60px; display: inline-block; border: 2px solid; font-size: 20px; outline: 0; } #subscribe button[id="select"],[id="select1"]{ background: #ecf0f1; width: 370px; height: 60px; /*padding-left: 100px;*/ /*padding-right: 100px;*/ padding: 0 10px; vertical-align: middle; font-size: 20px; outline: 0; color: #1169ff; border: 2px solid; text-align:center; } #subscribe input[id="main"] { width: 150px; outline: 0; } #triangle_down { position:absolute; width: 0; height: 0; border-top: 15px solid #1169ff; border-left: 9px solid transparent; border-right: 9px solid transparent; top:125px; left:90px; } #triangle_down1 { position:absolute; width: 0; height: 0; border-top: 20px solid #ecf0f1; border-left: 11px solid transparent; border-right: 11px solid transparent; top:125px; left:88px; } #triangle_down3 { position:absolute; width: 0; height: 0; border-top: 15px solid #1169ff; border-left: 9px solid transparent; border-right: 9px solid transparent; top:125px; left:464px; } #triangle_down2 { position:absolute; width: 0; height: 0; border-top: 20px solid #ecf0f1; border-left: 11px solid transparent; border-right: 11px solid transparent; top:125px; left:462px; } 
 <div id="subscribe"> <form action="" method="post" onsubmit=""> <p> <button type="button" id="select">Acheter</button> <button type="button" id="select1">Louer</button> </p> <!-- Using 2 traingles to achieve border outline :) --> <i id="triangle_down1" style="display: none"><i></i></i> <i id="triangle_down" style="display: none"><i></i></i><br/> <!--Louer triangles --> <i id="triangle_down2" style="display: none"><i></i></i> <i id="triangle_down3" style="display: none"><i></i></i><br/> <div></div> <p><input name="" placeholder="Entrer une ville au Maroc" type="text" id="landing-entry"/> <a href="/results"> <input type="submit" id="main" value="Search"/></a> </p> </form> 

Thanks 谢谢

this does it, look into jquery toggle or other ways of doing this as your code is very hard to work with at the mo. 为此,请查看jquery切换或其他执行此操作的方法,因为您的代码很难立即使用。 added $('#triangle_down2').add('#triangle_down3').hide(); 添加$('#triangle_down2').add('#triangle_down3').hide(); in the first click f 第一次点击f

 $(document).ready(function() { $('#select').click(function(){ $(this).css('background','#1169ff'); $(this).css('color','#ecf0f1'); $('#triangle_down').add('#triangle_down1').show(); if($('#select1').css('background','#1169ff') && $('#select1').css('color','#ecf0f1')){ $('#select1').css('background','#ecf0f1'); $('#select1').css('color','#1169ff'); $('#triangle_down2').add('#triangle_down3').hide(); } else if($('#triangle_down2').add('#triangle_down3').addClass('hidden')){ $('#triangle_down').add('#triangle_down1').show(); } }); $('#select1').on('click', function() { $(this).css('background','#1169ff'); $(this).css('color','#ecf0f1'); $('#triangle_down').add('#triangle_down1').hide(); $('#select').css('background','#ecf0f1'); $('#select').css('color','#1169ff'); $('#triangle_down2').add('#triangle_down3').show(); }); }) 
 #subscribe p{ text-align:center; } #subscribe input { background: rgba(255, 255, 255, 0.52); color: #666666; vertical-align: middle; width: 593px; border: 1px solid rgba(255, 255, 255, 0.76); padding: 0 10px; height: 60px; font-size: 20px; outline: 0; } #subscribe input[type="submit"]{ background: rgb(255, 135, 19); color: #ecf0f1; /*width: auto;*/ padding: 14px 25px; cursor: pointer; margin-left: 50px font-weight: bold; height: 60px; display: inline-block; border: 2px solid; font-size: 20px; outline: 0; } #subscribe button[id="select"],[id="select1"]{ background: #ecf0f1; width: 370px; height: 60px; /*padding-left: 100px;*/ /*padding-right: 100px;*/ padding: 0 10px; vertical-align: middle; font-size: 20px; outline: 0; color: #1169ff; border: 2px solid; text-align:center; } #subscribe input[id="main"] { width: 150px; outline: 0; } #triangle_down { position:absolute; width: 0; height: 0; border-top: 15px solid #1169ff; border-left: 9px solid transparent; border-right: 9px solid transparent; top:125px; left:90px; } #triangle_down1 { position:absolute; width: 0; height: 0; border-top: 20px solid #ecf0f1; border-left: 11px solid transparent; border-right: 11px solid transparent; top:125px; left:88px; } #triangle_down3 { position:absolute; width: 0; height: 0; border-top: 15px solid #1169ff; border-left: 9px solid transparent; border-right: 9px solid transparent; top:125px; left:464px; } #triangle_down2 { position:absolute; width: 0; height: 0; border-top: 20px solid #ecf0f1; border-left: 11px solid transparent; border-right: 11px solid transparent; top:125px; left:462px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="subscribe"> <form action="" method="post" onsubmit=""> <p> <button type="button" id="select">Acheter</button> <button type="button" id="select1">Louer</button> </p> <!-- Using 2 traingles to achieve border outline :) --> <i id="triangle_down1" style="display: none"><i></i></i> <i id="triangle_down" style="display: none"><i></i></i><br/> <!--Louer triangles --> <i id="triangle_down2" style="display: none"><i></i></i> <i id="triangle_down3" style="display: none"><i></i></i><br/> <div></div> <p><input name="" placeholder="Entrer une ville au Maroc" type="text" id="landing-entry"/> <a href="/results"> <input type="submit" id="main" value="Search"/></a> </p> </form> 

The main issue is that you're using .addClass('hidden') while using .show() 主要问题是您在使用.addClass('hidden')时正在使用.addClass('hidden') .show()

jQuery is aa bit fickle when it comes to show, hide, and toggle, but they impose an inline style on that element to either display: hidden or display: show . jQuery在显示,隐藏和切换时有些变幻无常,但它们在该元素上施加了内联样式以display: hiddendisplay: show Your class 'hidden' is being overriden by the in line style placed by jQuery. jQuery放置的内嵌样式将覆盖您的类“隐藏”。

Just be consistent when you're showing and hiding things. 在显示和隐藏内容时保持一致。 Either use in line styles, such as adding/removing classes of 'hidden' or 'shown', or using the .show() , .hide() , and .toggle() functions from jQuery. 在线条样式,如添加/移除的“隐藏”或类要么使用“”所示,或者使用.show() .hide().toggle()从jQuery函数。

I cleaned up the code, and put it all in a jsfiddle. 我清理了代码,并将其全部放入jsfiddle中。 Hope this helps https://jsfiddle.net/agayn7yn/ 希望这对https://jsfiddle.net/agayn7yn/有帮助

Youre not hiding the triangle divs, youre just adding a class 'hidden' which doesnt actually apply any css properties. 您没有隐藏三角形div,您只是添加了一个“隐藏”类,该类实际上并未应用任何CSS属性。

instead of addClass('hidden') you want to use hide() 您要使用hide()而不是addClass('hidden') hide()

this will use inline styling to show/hide the div which insures the highest level of css specificity 这将使用内联样式显示/隐藏div,以确保最高级别的CSS特异性

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

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