jquery函数,参数不起作用

时间:2013-01-03 07:49:41

标签: jquery

我已经使用参数创建了一个函数,但它不起作用。

这是我的代码:

 function hideQuestion( _hideQuestion, _showQuestion){
   $('#_showQuestion').removeClass('hideOnInit');
   $('#_hideQuestion').addClass('hideOnInit');
   }

如果我使用这样的功能

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion(question1, question2);' data-role='button'></a>

......没有任何反应。

我该如何解决?

3 个答案:

答案 0 :(得分:16)

您需要使用函数parameter变量,而您正在使用constant string。同时从ids传递onclick作为搅拌常量。

function hideQuestion( _hideQuestion, _showQuestion){
   $('#' + _showQuestion).removeClass('hideOnInit');
   $('#' + _hideQuestion).addClass('hideOnInit');
}

更改

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion(question1, question2);' data-role='button'></a>

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion("question1", "question2");' data-role='button'></a>

在JavaScript中,字符串可以包含在singledouble引号中,并且可以像我们在onclick

上一样进行组合
 onclick='hideQuestion("question1", "question2");' 

答案 1 :(得分:0)

你有一个功能和一个参数很好..但你没有在你的功能中使用该参数... 试试这个。

function hideQuestion( _hideQuestion, _showQuestion){
   $('#'+_showQuestion).removeClass('hideOnInit');  //using the set variable here...
   $('#'+_hideQuestion).addClass('hideOnInit');

}

和你的html,哟需要传递参数..这里我发送一个字符串

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion("question1", "question2");' data-role='button'></a>  // sending thevariable to the functin as parameter

答案 2 :(得分:0)

Adil的解决方案应该有效。您的问题是您使用代码搜索未声明或有兴趣查找的变量"#_showQuestion""#_hideQuestion"