简体   繁体   English

单击链接时显示div边框

[英]show div border when link is clicked

I'm trying to figure out how to show the top border on a footer, once a link is clicked in mentioned footer (it shows a modal in the website, not the jsfiddle). 我试图弄清楚如何在脚注中单击链接后在脚注中显示顶部边框(它显示了网站中的模式,而不是jsfiddle)。 It should also disappear when the link is clicked again. 再次单击链接时,它也应消失。 Once I apply the display:none; 一旦我应用display:none; which would trigger the js to show the border, everything fails to function. 这将触发js显示边框,所有功能均无法正常运行。 Any help appreciated, thanks. 任何帮助表示赞赏,谢谢。

jsfiddle: https://jsfiddle.net/0gtk60gz/ jsfiddle: https ://jsfiddle.net/0gtk60gz/

HTML 的HTML

<footer class="border">
  <h1 id="linkone">
    <a href="http://www.google.com">
      test
    </a>
  </h1>
    <h1 id="linktwo">
    <a href="http://www.google.com">
      test
    </a>
  </h1>
</footer>

CSS 的CSS

footer {
    position: absolute;
    left: 0;
    bottom: 0;
    width: 100%;
    color:black;
    background-color:blue;
    padding:15px;
}

.border {
  border-top:4px solid red;
  /**display:none:**/
}

h1,a {
  color:white;
}

JS JS

$('h1.linkone')
.on('click', function (event) {
    $('.border').fadeIn(100);

})
.on('click', function (event) {
    $('.border').fadeOut(100);
});


$('h1.linktwo')
.on('click', function (event) {
    $('.border').fadeIn(100);

})
.on('click', function (event) {
    $('.border').fadeOut(100);
});

If you are ready to change your html little bit like the below code it will help you to make your requirement working. 如果您准备好像下面的代码那样稍微更改html,它将帮助您使要求生效。

HTML 的HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1  /jquery.min.js"></script>
<footer class="border">
 <h1 id="linkone">
   <a href="http://www.google.com">
  test
 </a>
 </h1>
   <h1 id="linktwo">
   <a href="http://www.google.com">
     test
   </a>
  </h1>
</footer>

CSS 的CSS

<style type="text/css">
footer {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
color:black;
background-color:blue;
padding:15px;
}

 .border {
  border-top:4px solid red;
 /**display:none:**/
 }

h1,a {
color:white;
}
</style>

JS JS

<script>
  $( document ).ready(function() {
  $( "h1" ).click(function() {
  console.log('ok');
  $('footer').toggleClass( 'border');
  });
  });

</script>

You can see the demo Here 您可以在此处查看演示

You have the class .border assigned to the footer. 您已将.border类分配给页脚。 So if you put display: none to the footer, the .border won't show either... To work around this wrap a div around the footer and add the class .border to that div. 因此,如果您将display:none放在页脚上,则.border也不会显示...要解决此问题,请在footer上环绕一个div并将类.border添加到该div中。

You need to change the existing JS code to this one. 您需要将现有的JS代码更改为此代码。

$('h1').click(function(event) {
  $('footer').toggleClass('border');
});

It will show and hide the border when you click the link. 单击链接时,它将显示和隐藏边框。

Also do not forget to remove class="border" from footer tag, if you need to show border only when the link is clicked and not by default. 另外,如果只需要在单击链接时才显示边框,而不是默认情况下,也不要忘记从footer标记中删除class="border"

And you can also remove display: none from .border , which you commented. 您还可以删除display: none .border display: none .border ,您已对其进行注释。 Because it will hide the whole footer, of course, if your attempt was to use this CSS property for showing and hiding border . 当然,因为如果您尝试使用此CSS属性显示和隐藏border ,它将隐藏整个页脚。

Here is the working codepen 这是工作码本

UPD If you want to keep the animation effect without changing the existing code a lot, you will need to: UPD如果要在不改变现有代码的情况下保持动画效果,则需要:

  1. Return class="border" to footer class="border"返回到footer
  2. Add .toggle-border { border-top: 4px solid red;} to CSS styles .toggle-border { border-top: 4px solid red;}到CSS样式
  3. Update JS code to this one 将JS代码更新为此

    $('h1').click(function(event) { $('footer').toggleClass('toggle-border', 600, 'linear'); });

Check the codepen, I've updated it. 检查codepen,我已经更新了。 Also I've added jquery-ui script to external JS. 另外,我已将jquery-ui脚本添加到外部JS。

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

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