我有一个按钮,它应该是ajax的助手,它们应该被委托到它的容器上:
jQuery('#container').on('click', 'a', function() {
if(jQuery(this).hasClass('myButton1') {
//handle
return false;
} else if(jQuery(this).hasClass('myButton2') {
//handle
return false;
}
})所以我想要改进这个:
jQuery('#container').on('click', 'a.sharedClass', function() {
if(jQuery(this).hasClass('myButton1') {
//handle
return false;
} else if(jQuery(this).hasClass('myButton2') {
//handle
return false;
}
})您认为,如果:
发布于 2013-12-28 21:12:13
这是一个有趣的问题,我通常是如何使用HTML5 data属性来处理这个问题的。我会使用一个类似于so data-actionFn = 'delete'的数据属性。
The two below trigger ajax calls onclick
<a href="#" data-actionFn='delete' class='js-ajax'> delete </a>
<a href="#" data-actionFn='vote' class='js-ajax'> vote </a>
....
This does not trigger an ajax call
<a href="#"> do something </a>JAVASCRIPT
jQuery('#container').on('click', 'a.js-ajax', function (e) {
e.preventDefault();
var actionFn = $(this).data('actionFn'); // grab function name
if (window[actionFn]) {
window[actionFn]();
} else {
// throw Error here
}
});
function detele(){
// put code here
}
function favorite(){
// put code here
}
function spam(){
// put code here
}
function open(){
// put code here
}
function media(){
// put code here
}
function lightbox(){
// put code here
}
function vote(){
// put code here
}
function view(){
// put code here
}NB:,这只是给您一个如何实现它的想法。
https://stackoverflow.com/questions/20818749
复制相似问题