我有一个函数,我想用于两个非常类似的操作(如和不同)。我相信我需要重新命名一些东西,但在网上搜索后,我找不到如此明显的东西。任何帮助,非常感谢。如何在javascript中重命名函数
剧本我想重新命名:
<script>
$(function() {
// get into the habit of caching the elements you will be re-using
var error = $('.error');
$('.button').click(function() {
error.hide();
// cache all the elements we'll be using
var contactForm1 = $(this).closest('.contact_form1'),
likesid = contactForm1.find('input[name=likesid]'),
likerid = contactForm1.find('input[name=likerid]');
unlikestatus = contactForm1.find('button1[name=unlikestatus]');
if (likesid.val() == '') {
// ...
likesid.focus();
return false;
}
if (likerid.val() == '') {
// ...
likesid.focus();
return false;
}
// easier to use object rather than string
// not sure where likestatus came from so it is ommitted
var data = { likesid: likesid.val(), unlikestatus: unlikestatus.val(), likerid: likerid.val() };
// short-hand method for $.ajax with POST
$.post('simpletest.php', data, function (res) {
// the rest
contactForm.html('<h5 class="message" style="color: #4EB44B;">unliked</h5>')
});
// no need to do any return false
});
});
</script>
要重命名的函数或脚本? – mkaatman
我不确定,我对JavaScript很陌生。我正在寻找任何可以让我使用相同代码两次的方法,只需更改一些东西 – sicksixstrings
将它们作为参数传递给您的函数 –