此工具增加强度值左键单击强度值,减少左键单击值。
这样可行,但右键单击以减小值时会出现上下文菜单。
我怎样摆脱它?
var Alexander =
{
Strength: "AlexanderStrengthVal",
Bonus: "AlexanderRemainingBonusVal",
Limits: {
Strength: 60,
}
};
function add(character, stat)
{
var txtNumber = document.getElementById(character[stat]);
var newNumber = parseInt(txtNumber.value) + 1;
var BonusVal = document.getElementById(character["Bonus"]);
if(BonusVal.value == 0) return;
var newBonus = parseInt(BonusVal.value) - 1;
BonusVal.value = newBonus;
txtNumber.value = newNumber;
}
function subtract(character, stat)
{
var txtNumber = document.getElementById(character[stat]);
var newNumber = parseInt(txtNumber.value) - 1;
if(newNumber < character.Limits[stat]) return;
var BonusVal = document.getElementById(character["Bonus"]);
var newBonus = parseInt(BonusVal.value) + 1;
BonusVal.value = newBonus;
txtNumber.value = newNumber;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table cellpadding='5' border='1' style="text-align:center; color:#ffffff; background-color:#444444; font-family:arial; font-size:14px">
<tr>
<td><b>Character</b></td>
<td><b>Strength</b></td>
<td><b>Spending Bonus</b></td>
</tr>
<tr>
<td>Alexander</td>
<td>
<input
id="AlexanderStrengthVal"
type="text" value="60"
style="width:30px; border:none; color:#ffffff; background-color:transparent; text-align:center"
onfocus="this.blur()"
onClick="add(Alexander, 'Strength')"
onContextMenu="subtract(Alexander, 'Strength');"
/>
</td>
<td>
<input
id="AlexanderRemainingBonusVal"
type="text"
value="30"
style="width:30px; border:none; color:#ffffff; background-color:transparent; text-align:center"
/>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:3)
您必须使用event.preventDefault()
来防止这种情况发生。
对于您的代码,您必须pass the triggering event object as a reference到您的函数subtract
。像这样:
onContextMenu="subtract(event, Alexander, 'Strength');" // This is in the HTML
function subtract(e, character, stat)
{
e.preventDefault();
// The rest of your code…
}
但请注意,你的代码很乱。首先,在JSBin本身中,它会输出一系列您应该查看的警告。另一件事,you shouldn't bind your event handlers inline。 Please separate your concerns
答案 1 :(得分:0)
可以试试这个
http://jsbin.com/OGoLuHa/1/edit
代码 * 更新 *而不是全身:
http://jsbin.com/ujaSiTu/1/edit
<table cellpadding='5' border='1' style="text-align:center; color:#ffffff; background-color:#444444; font-family:arial; font-size:14px" oncontextmenu="return false;">
<body oncontextmenu="return false;">
然后只会执行此代码。
function subtract(character, stat)
{
alert('Substract context Menu');
var txtNumber = document.getElementById(character[stat]);
alert(txtNumber);
var newNumber = parseInt(txtNumber.value) - 1;
alert(newNumber);
if(newNumber < character.Limits[stat]) return;
var BonusVal = document.getElementById(character["Bonus"]);
var newBonus = parseInt(BonusVal.value) + 1;
BonusVal.value = newBonus;
txtNumber.value = newNumber;
alert(newNumber);
}
答案 2 :(得分:0)
偷了这个from a previous answer,但改变了一点。给<td>
一个像...“力量”或者其他的id,然后尝试这个:
$(document).ready(function() {
$('#strength').bind('contextmenu', function(e) {
return false;
});
});
将它放在开头并在代码中包含jQuery。
您的JSbin