Jquery全局变量赋值和在javascript函数中的使用
我有一个使用默认值声明的全局变量 listId(请参阅下面的代码),然后我将其分配给 jq("#nav1 li a").click(function() 但是一旦任何其他 javascript 函数调用采用 使其反映更改后的值?
在这之后,listId 不反映更改后的值,而只是声明期间分配的默认值,如何
listId = 'x';
var jq=jQuery.noConflict();// for avoiding conflict
jq("#nav1").click(function(){
alert(this.id);
listId = this.id; //this.id is displayed in alert message
});
function pageSwitch(){
alert('on change id : '+listId);
//when called after the click function, this does not reflect changed values
}
I have a global variable listId(refer the below code) declared with a default value and then I am assigning it inside the jq("#nav1 li a").click(function() But once any of the other javascript function call takes place after this one, the listId does not reflect the changed value, instead its just the default value assigned during declaration. How can I make it reflect the changed values?
thanks
listId = 'x';
var jq=jQuery.noConflict();// for avoiding conflict
jq("#nav1").click(function(){
alert(this.id);
listId = this.id; //this.id is displayed in alert message
});
function pageSwitch(){
alert('on change id : '+listId);
//when called after the click function, this does not reflect changed values
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您提供的代码对我有用。 listId 变量已成功更改为“nav1”。我编写了一个小测试脚本,它将 listId 更改为 div 的 id onClick,然后使用一个函数将其更改回“x”。每次都有效。 点击此处进行测试。
The code you supplied worked for me. The listId variable was successfully changed to "nav1". I've written up a little test script, which changes listId to the div's id onClick, and then uses a function to change it back to "x". It works every time. Click here to test it.
感谢 Guyz 的帮助..实际上,该函数是通过单击 h:commandLink 来调用的..所以每次重新加载页面时,js 函数也是如此...我只是使用了 a4j:commandLink 并且工作正常.. .我应该展示完整的图片..抱歉..下次会小心的。
Thanks guyz for the help..actually the function was invoked on click of an h:commandLink..so each time the page got reloaded and so did the js functions...I just used the a4j:commandLink and it worked fine...I should have presented the full picture..Sorry for that..will take care next time.