我有一些奇怪的行为。我想在点击 span
标签时触发事件,比如说 #span
id。当我在控制台中运行此命令时,要测试:
$('#span').toggle(function(){
console.log('hi');
},function(){
console.log('hey');
});
#span
显示设置为无。我试图找到一些可能干扰的代码,但没有找到任何...
最佳答案
此版本的toggle was removed在 jQuery 1.9 中,所以如果您使用 jQuery >= 1.9 它将切换显示状态
启用已删除功能的一种可能的解决方案是添加 migration plugin jQuery 之后
另一种方法是自己编写一个toggleClick插件,如下所示this answer
$.fn.toggleClick = function(){
var methods = arguments, // store the passed arguments for future reference
count = methods.length; // cache the number of methods
//use return this to maintain jQuery chainability
return this.each(function(i, item){
// for each element you bind to
var index = 0; // create a local counter for that element
$(item).click(function(){ // bind a click handler to that element
return methods[index++ % count].apply(this,arguments); // that when called will apply the 'index'th method to that element
// the index % count means that we constrain our iterator between 0 and (count-1)
});
});
};
然后
$('#span').toggleClick(function(){
console.log('hi');
},function(){
console.log('hey');
});
关于javascript - jquery切换触发器显示为无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18987226/