在我的应用程序中,我有ck编辑器,里面有一些文字。用户可以使用F1
,F2
,F3
和F4
键选中文本,并取消此删除文本。
如果用户在使用f4键多次选择相同文本时选择了一些使用F1键的文本,则出现为负数。他们不应该消极,应该停在零。下面是连续按f4的屏幕截图。小行共同保险变为负数,有时PGS行变为负数。他们不应该变成消极的,应该停在零。我怎么能这样做?
下面是F3和F4键的javascript代码
if (event.data.$.keyCode == 114) {
label = document.getElementById('ctl00_ContentPlaceHolder1_lblMinorOccurences');
m = label.textContent.match(/^(.*?)(\d+)$/);
value = parseInt(m[2], 10);
value = isNaN(value) ? 0 : value;
if (start_element.$.innerHTML.search(/{.*}/) >= 0) {
var index = start_element.$.innerHTML.search(/{.*}/);
var str = null;
if (index != -1) {
str = start_element.$.innerHTML;
if (start_element.$.innerHTML.search("{0.5}") >= 0) {
label.textContent = m[1] + (value - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
return;
}
else if (start_element.$.innerHTML.search("{1.0}") >= 0) {
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblMajorOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#ffff66'); start_element.appendText("{0.5}");
label.textContent = m[1] + (value + 1)
return;
}
else if (start_element.$.innerHTML.search("{3.0}") >= 0) {
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblCriticalOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#ffff66'); start_element.appendText("{0.5}");
label.textContent = m[1] + (value + 1)
return;
}
else (start_element.$.innerHTML.search("{0.25}") >= 0)
{
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblPGSOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#ffff66'); start_element.appendText("{0.5}");
label.textContent = m[1] + (value + 1)
return;
}
}
}
else {
start_element.setStyle('background-color', '#ffff66'); start_element.appendText("{0.5}");
label.textContent = m[1] + (value + 1)
}
}
if (event.data.$.keyCode == 115) {
label = document.getElementById('ctl00_ContentPlaceHolder1_lblPGSOccurences');
m = label.textContent.match(/^(.*?)(\d+)$/);
value = parseInt(m[2], 10);
value = isNaN(value) ? 0 : value;
if (start_element.$.innerHTML.search(/{.*}/) >= 0) {
var index = start_element.$.innerHTML.search(/{.*}/);
var str = null;
if (index != -1) {
str = start_element.$.innerHTML;
if (start_element.$.innerHTML.search("{0.5}") >= 0) {
label.textContent = m[1] + (value - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
return;
}
else if (start_element.$.innerHTML.search("{1.0}") >= 0) {
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblMajorOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#0bff01'); start_element.appendText("{0.25}");
label.textContent = m[1] + (value + 1)
return;
}
else if (start_element.$.innerHTML.search("{3.0}") >= 0) {
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblCriticalOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#0bff01'); start_element.appendText("{0.25}");
label.textContent = m[1] + (value + 1)
return;
}
else (start_element.$.innerHTML.search("{0.5}") >= 0)
{
lblOther = document.getElementById('ctl00_ContentPlaceHolder1_lblMinorOccurences');
mOther = lblOther.textContent.match(/^(.*?)(\d+)$/);
valOther = parseInt(mOther[2], 10);
valOther = isNaN(valOther) ? 0 : valOther;
lblOther.textContent = mOther[1] + (valOther - 1);
var txt = str.replace(/{.*}/, "");
start_element.$.innerHTML = txt;
start_element.setStyle('background-color', '#ffffff');
start_element.setStyle('background-color', '#0bff01'); start_element.appendText("{0.25}");
label.textContent = m[1] + (value + 1)
return;
}
}
}
else {
start_element.setStyle('background-color', '#0bff01'); start_element.appendText("{0.25}");
label.textContent = m[1] + (value + 1)
}
}
主要是当我在使用其他键
选择的文本上重复使用F4键时答案 0 :(得分:0)
要在零处停止,您需要使用简单的if条件检查当前值,或者您可以使用逻辑运算符来减少代码中的行数。
(value && value - 1)
如果值为0
,则上述代码返回0
,否则减去它的值。这是因为0
是假值并且没有进一步执行而只是它返回。结果代码如下:
label.textContent = m[1] + (value && value - 1)
因此无论你在哪里递减,价值都是一样的。或者您可以使用三元运算符或简单条件来检查。例如:
if(value != 0) value--; // decrements value if not 0
label.textContent = m[1] + value; // update with the value