我正在尝试使用 jquery 根据单个点击事件的条件执行多个函数。
这是我的代码:
$(document).ready( function (){
var Acco_head = $('#Dashbordacco > li > a');
var Acco_body = $('#Dashbordacco > li > div');
Acco_head.first().addClass('active').next().show();
Acco_head.on('click', function(event){
if(myScroll != undefined || myScroll != null) {
refreshScroller(myScroll);
}
event.preventDefault();
if ($(this).attr('class') != 'active'){
Acco_body.slideUp('normal');
if ($(this).attr('class') = 'UE') {
callPortfolioDataUrl(PortfolioDataUrl)
}
$(this).next().stop(true,true).slideToggle('normal');
Acco_head.removeClass('active');
$(this).addClass('active');
}
});
});
这是 HTML:-
<li class="UE"><a href="#">Upcoming Events</a>
<div id="indexdata2">
</div></li>
但它会抛出一个错误,例如:“赋值的左侧不是引用。”。
有什么帮助吗?
最佳答案
使用比较运算符
if ($(this).attr('class') === 'UE') {
代替赋值运算符
。
if ($(this).attr('class') = 'UE') {
尝试,
$(document).ready( function (){
var Acco_head = $('#Dashbordacco > li > a');
var Acco_body = $('#Dashbordacco > li > div');
Acco_head.first().addClass('active').next().show();
Acco_head.on('click', function(event){
if(myScroll != undefined || myScroll != null) {
refreshScroller(myScroll);
}
event.preventDefault();
if ($(this).hasClass('active')){
Acco_body.slideUp('normal');
if ($(this).hasClass('UE')) {
callPortfolioDataUrl(PortfolioDataUrl)
}
$(this).next().stop(true,true).slideToggle('normal');
Acco_head.removeClass('active');
$(this).addClass('active');
}
});
});
编辑:
使用 .hasClass('classname')
检查源元素是否有特定的类
关于javascript - 单击事件的多个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20697095/