下面的javascript jQuery代码可以工作,除了我想在按钮的状态中添加2个功能。
当用户点击其中一个按钮时,未点击的另一个按钮会获得一个新类(外观)。
两个按钮的状态都应该变为无法点击。
[div id="1" class="__button_image"] [/div] [div id="2" class="__button_image"] [/div]
$("div.__button_image").mouseover(function () { $(this).addClass("__button_image_hover"); }); $("div.__button_image").mouseout(function () { jq(this).removeClass("__button_image_hover"); }); $("div.__button_image").click(function () { $(this).removeClass("__button_image_hover"); $(this).addClass("__button_image_clicked"); jQuery.get('/do/request'); });
答案 0 :(得分:3)
以下是我使用的最终代码:
$("div.__button_image").mouseover(function () {
$(this).addClass("__button_image_hover");
});
$("div.__button_image").mouseout(function () {
$(this).removeClass("__button_image_hover");
});
$("div.__button_image").click(function () {
/** change button look to 'clicked' */
$(this).addClass("__button_image_clicked");
/** get the id of the current button */
b_id = $(this).attr('id');
/** unbind both vote buttons for *no* interaction */
$("div.__button_image").unbind('click');
$("div.__button_image").unbind('mouseover');
$("div.__button_image").unbind('mouseout');
/**
* wire the .each function to iterate the classes
* so we can change the look of the one that was
* not clicked.
*/
$('div.__button_image').each(function() {
button_id = $(this).attr('id');
if(button_id!=b_id) {
$('#'+button_id).removeClass("__button_image");
$('#'+button_id).addClass("__button_image_gray");
}
});
jQuery.get('/do/request?id='+b_id);
$(this).parent().css('cursor', 'default');
答案 1 :(得分:2)
有什么问题?我唯一能看到你缺少的是
$("div.__button_image").unbind('click');
这将删除'click'处理程序(将其设置回默认值)。
答案 2 :(得分:1)
我会将你的click()处理程序更改为:
$("div.__button_image").click(function () {
$(this).removeClass("__button_image_hover");
$(this).addClass("__button_image_clicked");
/*
* Add look class to all buttons, then remove it from this one
*/
$("div.__button_image").addClass("look");
$(this).removeClass("look");
/*
* Remove click handler from all buttons
*/
$("div.__button_image").unbind('click');
jQuery.get('/do/request');
});
答案 3 :(得分:1)
这总是适用于我(也将不透明度更改为80%并将光标更改为等待)
$("#buttonDivId").css({opacity: 0.8, cursor: "wait"}).prop("disabled", true);