OP评论后更新
HTML
包含 jQuery 库文件
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
使用插件https://github.com/fkling/jQuery-Function-Toggle-Plugin --> 它接受任意数量的函数,可用于任何事件。
演示
你想要这样的东西。
<script type="text/javascript">
/*
* jQuery Function Toggle Pluing
* Copyright 2011, Felix Kling
* Dual licensed under the MIT or GPL Version 2 licenses.
*/
(function ($) {
$.fn.funcToggle = function (type, data) {
var dname = "jqp_eventtoggle_" + type + (new Date()).getTime(),
funcs = Array.prototype.slice.call(arguments, 2),
numFuncs = funcs.length,
empty = function () {},
false_handler = function () {
return false;
};
if (typeof type === "object") {
for (var key in type) {
$.fn.funcToggle.apply(this, [key].concat(type[key]));
}
return this;
}
if ($.isFunction(data) || data === false) {
funcs = [data].concat(funcs);
numFuncs += 1;
data = undefined;
}
funcs = $.map(funcs, function (func) {
if (func === false) {
return false_handler;
}
if (!$.isFunction(func)) {
return empty;
}
return func;
});
this.data(dname, 0);
this.bind(type, data, function (event) {
var data = $(this).data(),
index = data[dname];
funcs[index].call(this, event);
data[dname] = (index + 1) % numFuncs;
});
return this;
};
}(jQuery));
$('#chnage_p').funcToggle('click', function () {
$('#example1div').text('Content 1');
}, function () {
$('#example1div').text('Content 2');
}, function () {
$('#example1div').text('Content 3');
}, function () {
$('#example1div').text('Content 4');
});
</script>