简体   繁体   English

如何从外部链接更改标记的CSS

[英]How to change css of tag from an outside link

I have a menu in a tag called sf-menu . 我在一个名为sf-menu的标签中有一个sf-menu

I need the visibility to change to none when the link is clicked and toggled back when clicked again. 我需要在单击链接时将可见性更改为none ,并在再次单击时切换回来。

Can I achieve this with CSS only or do I need JavaScript? 我可以只使用CSS实现这一点还是需要JavaScript?

Hoping someone can help me with an example. 希望有人可以帮我一个例子。

.sf-menu {visibility: visible}
<a class="closed" href="#sidewidgetarea"> Switch</a>
  1. You can use input type='checkbox' for it : 您可以使用input type ='checkbox':

http://jsfiddle.net/gSPqX/1/ http://jsfiddle.net/gSPqX/1/

<input type="checkbox" style="display: none" id="cb">
<label for="cb">Click Here</label>
<div>
    Hello. This is some stuff.
</div>
  1. One more better solution using :target 一个更好的解决方案使用:target

 #menu .menu{ display:none; } #menu:target .menu{ display: block; } #menu:target .menu__open{ display: none; } #menu .menu__close{ display: none; } #menu:target .menu__close{ display: block; } 
 <div id="menu"> <a href="#menu" class="menu__open">Open menu</a> <a href="#" class="menu__close">Close menu</a> <div class="menu"> Hello. This is some stuff. </div> </div> 

If you are using jquery, you can use jquery.toggle() 如果您使用的是jquery,则可以使用jquery.toggle()

Example: 例:

$(".closed").click(function(event) {
    event.stopPropagation()
    $(".sf-menu").toggle();
});

Source: https://api.jquery.com/toggle/ 资料来源: https//api.jquery.com/toggle/

You can do it using only CSS. 你只能使用CSS来做到这一点。 The trick is to leave a trail that could be clicked and work as a switch/button for turning display on or off. 诀窍是留下可以点击的路径,并作为开关/按钮用于打开或关闭显示。 You can accomplish that by wrapping your content needed to be toggled into some div or any other element suitable to you. 您可以通过将需要切换的内容包装到某个div或任何其他适合您的元素中来实现。

Here is a little example for that. 这是一个小例子。

 label { cursor: pointer; } #menu-toggle { display: none; /* hide the checkbox */ } #menu { display: none; } #menu-toggle:checked + #menu { display: block; } 
 <label for="menu-toggle">Click me to toggle menu</label> <input type="checkbox" id="menu-toggle"/> <ul id="menu"> <li><a href="#">First link</a></li> <li><a href="#">Second link</a></li> <li><a href="#">Third link</a></li> </ul> 

Here is a super simple solution in jQuery, using the tags you have mentioned. 这是jQuery中一个超级简单的解决方案,使用您提到的标签。 But first remove your CSS-row! 但首先删除你的CSS行!

Please not that this example doesnät seem to execute on stackoverflow.com but works fine on jsfiddle.net. 请注意,这个示例似乎没有在stackoverflow.com上执行,但在jsfiddle.net上运行正常。

 $('.closed').click(function(){ $('.sf-menu').toggle(); }); 
 <a class="closed" href="#sidewidgetarea">Switch</a> <div class="sf-menu">The menu</div> 

In HTML : 在HTML中:

<h1 class="sf-menu"> TestText </h1>

JavaScript : JavaScript:

 var sfmenu = document.getElementsByClassName("sf-menu");
sfmenu[0].onclick = function () {
    if (this.style.display == 'none' ) {
        this.style.display = '';
    } else {
        this.style.display = 'none';
    }
};

If you're able to edit the DOM, there is a PureCSS solution (see below), but I think that the best solution is using Javascript, jQuery offers you a crossbrowser solution! 如果您能够编辑DOM,那么有一个PureCSS解决方案(见下文),但我认为最好的解决方案是使用Javascript,jQuery为您提供了一个跨浏览器解决方案!

Hope Helps!! 希望有助于!!

 var HIDDEN_CLASS = 'sf-menu-hidden'; /** jQuery **/ jQuery(document).ready(function($) { $('#jQueryTest .closed').click(function closeWithJQuery(event) { event.preventDefault(); $('#jQueryTest .sf-menu').toggleClass(HIDDEN_CLASS); }); }); /** VanillaJS **/ document.addEventListener("DOMContentLoaded", function(event) { document .querySelector('#VanillaJSTest .closed') .addEventListener('click', function() { var el = document.querySelector('#VanillaJSTest .sf-menu'); var task = el.classList.contains(HIDDEN_CLASS) ? 'remove' : 'add'; el.classList[task](HIDDEN_CLASS); }); }); 
 * { -webkit-user-select: none; user-select: none; } .sf-menu-hidden { visibility: hidden; } a, label { cursor: pointer; } article + article { margin-top: 10px; border-top: 1px solid green; } #PureCSSTest [type="checkbox"] { display: none; } #PureCSSTest [type="checkbox"]:checked + .sf-menu { visibility: hidden; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <article id="jQueryTest"> <h1>Test jQuery</h1> <a class="closed">TOGGLE MENù</a> <div class="sf-menu"> <ul> <li> Hey I Am On THE SCREEN</li> </ul> </div> </article> <article id="VanillaJSTest"> <h1>VanillaJS</h1> <a class="closed">TOGGLE MENù</a> <div class="sf-menu"> <ul> <li> Hey I Am On THE SCREEN</li> </ul> </div> </article> <article id="PureCSSTest"> <h1>PURE CSS</h1> <a class="closed"><label for="toggleClosed">TOGGLE MENù</label></a> <input type="checkbox" id="toggleClosed"> <div class="sf-menu"> <ul> <li> Hey I Am On THE SCREEN</li> </ul> </div> </article> 

CSS: CSS:

.myVisibleLink{
     display:block;
}
.myHiddenLink{
     display:none;
}

Jquery: jQuery的:

var myFlag = false;
$(function() {                       //run when the DOM is ready
  $("#IDOfLink").click(function() {  //use a class, since your ID gets mangled
      if(myFlag == false){    
           myFlag = true;
           $(this).addClass("myHiddenLink");      //add the class to the clicked element
      }else{
           myFlag = false;
           $(this).addClass("myVisibleLink");      //add the class to the clicked element
     }
  });
});

You can achieve this with pure Javascript . 您可以使用纯Javascript实现此目的。

Use the onclick event for the anchor tag. 使用onclick事件作为锚标记。

 function OnsidewidgetareaClick() { var elementObj = document.getElementsByClassName("sf-menu")[0]; if (elementObj.style.display == 'block' ||elementObj.style.display=='') { elementObj.style.display = 'none'; } else { elementObj.style.display = 'block'; } } 
 .sf-menu {visibility: visible} 
 <a class="closed" href="#sidewidgetarea" onclick="OnsidewidgetareaClick();"> Switch</a> <div class="sf-menu"> This is the .sf-menu element. </div> 

Maybe this can help.Using this will hide your menu on click and then as per your requirement remove or add class afterwards. 也许这可以提供帮助。使用此功能会在点击时隐藏您的菜单,然后根据您的要求删除或添加课程。

#sf-menu:active{
    display:none;
}

I like the handy "checkbox" solution in responses, pure CSS. 我喜欢回答中方便的“复选框”解决方案,纯CSS。 However if you need the link to be involved, you may try to utilize the onclick attribute in the link HTML code. 但是,如果您需要涉及链接,则可以尝试使用链接HTML代码中的onclick属性。

 #sf-menu { visibility: hidden; opacity: 0; transition: all .3s; } 
 <div id="#sidewidgetarea"> <a class="closed" href="#sidewidgetarea" onclick="with(document.getElementById('sf-menu').style){ visibility=(visibility==='visible'&&!(opacity=0)&&'hidden') ||((opacity=1)&&'visible')}" >Switch</a> <ul id="sf-menu"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul> </div> 

 #checkbox1 { display:none; } #checkbox1:checked ~ .sf-menu { visibility: visible; } .sf-menu { visibility:hidden; } .pseudo-link { cursor: pointer; } 
 <div> <input type="checkbox" id="checkbox1"> <label for="checkbox1"" class="pseudo-link>The link</label> <ul class="sf-menu"> <li> item 1</li> <li> item 2</li> <li> item 3</li> </ul> </div> 

No javascript required. 不需要JavaScript。 The label will be treated as if it's a link via css. 标签将被视为通过css链接。

Unfortunately, CSS do not have that feature yet. 不幸的是,CSS还没有这个功能。

JQuery can done for you, in two lines of code. JQuery可以通过两行代码为您完成。

$('.button').click(function(){
   $('.sf-menu').toggle();
});

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

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