Java脚本显示和隐藏
Java script to show and hide
我的要求很简单,它必须检查变量并相应地显示/隐藏类。它位于sharepoint发布页面上。使用下面的代码片段不起作用。
if ( source = 'show')
{
$('.classshide').hide();
}
else
{
$('.classsshow').hide();
}
它只在源变量为show时起作用,否则也应该起作用,当它不等于show或等于hide时,请隐藏类show。
你的相等性测试是错误的。
if ( source = 'show')
应该if ( source == 'show')
或者
if ( source === 'show') //if source is a string and you don't want type coercion
您需要使用相等(==
)或严格相等(===
)运算符来比较source
变量与if语句中的"show"。因为您提到需要显示和隐藏类,我猜您想要交替显示哪些类,所以我相应地调整了其余的代码。
if ( source === 'show' )
{
$('.classshide').hide();
$('.classsshow').show();
}
else if ( source === 'hide' )
{
$('.classsshow').hide();
$('.classshide').show();
}
else // for some reason source is neither 'show' or 'hide'
{ //default to the logic for if it is 'hide'
$('.classsshow').hide();
$('.classshide').show();
}
请使用严格比较===
。它更快,因为它在比较变量的value
时不需要转换type
。
编辑:
// get what the current state of hide/show is
var source = $('.classhide').length === 0 ? 'show' : 'hide';
if ( source === 'show') {
$('.classshide').hide();
} else {
$('.classsshow').hide();
}
相关文章:
- 基于其他下拉菜单选择隐藏/显示下拉菜单
- 数据表-隐藏/显示列
- $scope变量,ng隐藏/显示
- 隐藏/显示包含单词的ul li项目.但只能入住李的子女
- JQuery隐藏/显示无法正常工作
- 隐藏/显示下面的分区根据是否选择某个选项进行选择
- 使用类而不是id在JavaScript中隐藏显示
- 我想隐藏/显示我的SVG元素,同时在anguarJS中使用它
- 隐藏/显示图像
- Javascript 中的表 - 隐藏/显示列
- 仅使用Javascript隐藏/显示元素,而不会弄乱DOM
- 隐藏/显示 js 代码不会在 IE8 或更低版本上运行
- Magento:根据所选国家/地区隐藏/显示输入字段
- 货到付款选择隐藏/显示付款方式
- "隐藏/显示”;元素和更改按钮背景图像
- Bootstrap Collapse.js切换菜单的隐藏/显示
- 如何隐藏/显示<面板>使用jquery
- 在php中隐藏/显示切换回声结果
- 如何使用jquery使2个按钮切换css,而只使用1个按钮.不显示隐藏/显示
- JQuery隐藏/显示不起作用