Javascript 要显示和隐藏的Java脚本

Javascript 要显示和隐藏的Java脚本,javascript,jquery,sharepoint-2010,Javascript,Jquery,Sharepoint 2010,我的要求非常简单,它必须检查变量并相应地显示/隐藏类。它位于sharepoint发布页面上。 使用以下代码段无效 if ( source = 'show') { $('.classshide').hide(); } else { $('.classsshow').hide(); } 它只在源变量为show时有效,也应该以另一种方式工作,当它不等于show或hide时,请隐藏classshow 你的平等测试是错误的 if ( source = 'show') 应该是 if ( source =

我的要求非常简单,它必须检查变量并相应地显示/隐藏类。它位于sharepoint发布页面上。 使用以下代码段无效

if ( source = 'show')
{
$('.classshide').hide();
}
else
{
$('.classsshow').hide();
}

它只在源变量为show时有效,也应该以另一种方式工作,当它不等于show或hide时,请隐藏classshow

你的平等测试是错误的

if ( source = 'show')
应该是

if ( source == 'show')
也可能是

if ( source === 'show') //if source is a string and you don't want type coercion

请使用严格的比较
===
。它速度更快,因为在比较变量的
时,它不必转换
类型

编辑:


您需要使用相等(
=
)或严格相等(
==
)运算符将
变量与if语句中的“显示”进行比较。因为您提到需要显示和隐藏类,我猜您希望交替显示哪些类,所以我相应地调整了代码的其余部分

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();
}

您需要将
if(source='show')
更改为
if(source='show')
,如果我使用“==”来代替“=”,那么这在第一种情况下甚至不起作用。您确定
source
是正确的值吗?是的,它已经在页面上定义好了,我可以清楚地看到变量是show还是hide。我只是把它拉到我的java脚本中,检查它是显示还是隐藏。如果它确实定义正确,那么等式运算符不起作用真的很奇怪…不起作用。现在,它甚至不适用于第一种情况。什么都没有发生。@svs然后我感觉在比较中使用时,
源代码
没有用实际值定义。出于某种原因,==或===对我不起作用。如果我使用only=它至少检查第一个if条件,否则失败。你说它不工作是什么意思?您是否在if块之前设置变量,并将其设置为正确的值?变量正在页面中定义,我只是将其拉入脚本以签入if块。您是否可以放置
警报(源)在if块之前,让我知道它说了什么?它的定义方式可能有问题……它在“查看源代码”中显示,但当我添加警报时,它没有在警报中显示。
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();
}