单击外部菜单以关闭jquery(Click outside menu to close jquery)
我一直在一个响应式网站和平板电脑上成为下拉菜单,但我需要在菜单关闭时在文档之外进行点击,(我还需要鼠标箭头更改为正常形式)我不能找到一种方法,这是我一直在使用的代码:
JQUERY
$(function() { var btn_mobile = $('#nav-mobile'), menu = $('#menu').find('ul'); btn_mobile.on('click', function (e) { e.preventDefault(); e.stopPropagation(); var el = $(this); el.toggleClass('nav-active'); menu.toggleClass('open-menu'); }) });
HTML
<nav id="menu"><a class="nav-mobile" id="nav-mobile" href="#">MENU</a> <ul> <li><a href="index.html">Home</a></li> <li><a href="work.html">Work</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul>
PARTIAL CSS(平板电脑)
#nav-mobile{ display: none; background: url(../images/menu-icons.svg) no-repeat 42px -2px; float: right; width:75px; height:35px; padding-top:9px; position: absolute; right:0; top:0; font-weight:bold; } #nav-mobile.nav-active{opacity: 1; background: url(../images/menu-icons.svg) no-repeat 42px -48px;} /* TABLET */ #nav-mobile{display: block; } #menu{margin-top:0px;width: 100%;float: none;padding-top:55px;} #menu ul{ max-height: 0; overflow: hidden; text-align:center; position:relative; z-index:500; transition: max-height .5s, box-shadow 1.2s, opacity 0.5s; opacity:0; margin:0 -3.2%; } #menu li{background:#fff;border-bottom: 1px solid #ececec;float: none;} #menu li a{padding: 12px 0;height: auto;width:100%; text-transform:uppercase;} #menu li a:hover{background:#fbfbfb;} #menu ul.open-menu{max-height: 400px;transition: max-height .5s, box-shadow 1.2s, opacity 0.5s; border-top: 1px solid #CCC; box-shadow: 0px 9000px 0px 9000px rgba(0,0,0,0.15); opacity:1; }
感谢您的关注!!!。
I've been working a responsive web site and on tablet become dropdown menu but I need that when a click is made outside of the document the menu closes, (also I need that the mouse arrow changes to it's normal form) I can't find a way to do this, here is the code I have been using:
JQUERY
$(function() { var btn_mobile = $('#nav-mobile'), menu = $('#menu').find('ul'); btn_mobile.on('click', function (e) { e.preventDefault(); e.stopPropagation(); var el = $(this); el.toggleClass('nav-active'); menu.toggleClass('open-menu'); }) });
HTML
<nav id="menu"><a class="nav-mobile" id="nav-mobile" href="#">MENU</a> <ul> <li><a href="index.html">Home</a></li> <li><a href="work.html">Work</a></li> <li><a href="about.html">About</a></li> <li><a href="contact.html">Contact</a></li> </ul>
PARTIAL CSS (tablet)
#nav-mobile{ display: none; background: url(../images/menu-icons.svg) no-repeat 42px -2px; float: right; width:75px; height:35px; padding-top:9px; position: absolute; right:0; top:0; font-weight:bold; } #nav-mobile.nav-active{opacity: 1; background: url(../images/menu-icons.svg) no-repeat 42px -48px;} /* TABLET */ #nav-mobile{display: block; } #menu{margin-top:0px;width: 100%;float: none;padding-top:55px;} #menu ul{ max-height: 0; overflow: hidden; text-align:center; position:relative; z-index:500; transition: max-height .5s, box-shadow 1.2s, opacity 0.5s; opacity:0; margin:0 -3.2%; } #menu li{background:#fff;border-bottom: 1px solid #ececec;float: none;} #menu li a{padding: 12px 0;height: auto;width:100%; text-transform:uppercase;} #menu li a:hover{background:#fbfbfb;} #menu ul.open-menu{max-height: 400px;transition: max-height .5s, box-shadow 1.2s, opacity 0.5s; border-top: 1px solid #CCC; box-shadow: 0px 9000px 0px 9000px rgba(0,0,0,0.15); opacity:1; }
thank you for your attention!!!.
原文:https://stackoverflow.com/questions/24049708
最满意答案
我想你正在寻找
getPreferenceScreen()
您可以使用它来修改xml中定义的
PreferenceScreen
。例如:
private void disableHardKeyboardOptions() { Preference pref = getPreferenceScreen().findPreference("pref_hardkeyboard_option"); pref.setEnabled(false); pref.setSummary(custom.getString(R.string.pref_no_hardkeyboard)); }
I guess that you are looking for
getPreferenceScreen()
You can use it to modify the
PreferenceScreen
that was defined in the xml.For example:
private void disableHardKeyboardOptions() { Preference pref = getPreferenceScreen().findPreference("pref_hardkeyboard_option"); pref.setEnabled(false); pref.setSummary(custom.getString(R.string.pref_no_hardkeyboard)); }
相关问答
更多-
Android在PreferenceActivity中获得Preference的视图(Android get view of Preference in PreferenceActivity)[2022-11-23]
我不确定如何获取首选项的视图,但是如果要从屏幕中移除视图(设置View.gone的可见性),可以使用以下命令: getPreferenceScreen()。removePreference(thePreference) I'm not sure on how to get the view for a preference, but if you want to remove the view from the screen (set visibility to View.gone) you can use ... -
Android - 我应该继续使用PreferenceActivity吗?(Android - Should I keep using PreferenceActivity? [duplicate])[2022-03-24]
仅仅因为它被弃用并不意味着它停止工作 - 新设备仍然支持PreferenceActivity而没有问题。 然而,在较大的设备上(例如10“平板电脑), PreferenceFragment / PreferenceHeaders的双窗格支持看起来好多了。在Google将它们添加到支持库之前,您可以使用第三方库(如UnifiedPreferences)为所有人Android版本。 Just because it is deprecated does not mean it stops working - ne ... -
来自PreferenceActivity的Android SharedPreferences(Android SharedPreferences from PreferenceActivity)[2021-12-07]
SharedPreferences是在应用程序的许多部分之间共享数据的一种方式。 一旦有人引用了共享首选项,他们可以像这样读取它: int currlist = prefs.getInt("currlist", 0); float goal = prefs.getFloat("goaltxt", 0); 他们还可以注册接收数据已更改的通知: prefs.registerOnSharedPreferenceChangeListener(this); 他们的onSharedPreferenceChang ... -
import android.preference.PreferenceManager; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // then you use prefs.getBoolean("keystring", true); 更新 根据共享首选项| Sai Geetha MN的Android开发人员教程(第13部分) 许多应用程序可以提供一种方式来捕获特定应用程序或活动的设置的用 ...
-
在方法的描述中没有提供替代方法,因为首选方法(从API级别11开始)要实例化PreferenceFragment对象以从资源文件加载您的首选项。 请参阅示例代码: PreferenceActivity No alternative method is provided in the method's description because the preferred approach (as of API level 11) is to instantiate PreferenceFragment obje ...
-
在这里找到它: 如何从Android中的PreferenceActivity获取SharedPreferences? 在NumberPickerReferenceActivity中:替换 preferenceManager.setSharedPreferencesMode(MODE_PRIVATE); 通过 preferenceManager.setSharedPreferencesMode(MODE_WORLD_READABLE); 并替换 prefs = PreferenceManager.getD ...
-
在您的主要活动中,您可以执行以下操作 SharedPreferences prefs = getSharedPreferences("YOUR_PREFERENCES_KEY",false); boolean isSetToTrue = prefs.getBoolean("YOUR_BOOLEAN_KEY",false); if(isSetToTrue==false){ //here set your checkBox to true } main ...
-
我想你正在寻找getPreferenceScreen() 您可以使用它来修改xml中定义的PreferenceScreen 。 例如: private void disableHardKeyboardOptions() { Preference pref = getPreferenceScreen().findPreference("pref_hardkeyboard_option"); pref.setEnabled(false); pref.setSummary(custom.g ...
-
我无法弄清楚如何使用Android PreferenceActivity(I can't figure out how to use an android PreferenceActivity)[2023-08-16]
getDefaultSharedPreferences(Context)接受Context实例(如它所说),并且您提供的内容不是上下文。 如果您的FiscalFrequency类位于activity中且不是静态的,请尝试调用 getDefaultSharedPreferences(UserPrefs.this) 或者您当前的任何活动(因为活动扩展了上下文) 否则,在创建FiscalFrequency类的实例时提供Context实例。 一种方法是通过构造函数来完成。 如果您要发布代码的一部分,那么您将更容易 ... -
不要担心,使用pref.getTitle().equals("Default as grid")工作正常。 Don't worry, using pref.getTitle().equals("Default as grid") worked fine.