我已将此代码添加到我的wordpress博客的functions.php中:
var oldselection = selection;
它工作正常,但我想缩短复制的代码。
我替换了这一行
var oldselection = selection.toString().substr(0,500);
到
var button1 = document.getElementById("start");
var button2 = document.getElementById("stop");
var timegraph = document.getElementById("ceas");
var lapButton = document.getElementById("lap");
var lame = document.getElementById("test");
var time = 0;
var ResetStart = 0;
var myInterval;
var body = document.getElementById("body");
var action = document.getElementById("lappara");
function Start() {
if (ResetStart == 0) {
ResetStart = 1;
Running();
button1.innerHTML = "Pause";
}
else {
ResetStart = 0;
button1.innerHTML = "Resume";
clearInterval(myInterval);
}
}
function Reset() {
time = 0;
ResetStart = 0;
button1.innerHTML = "Start";
timegraph.innerHTML = "00:00:00:00";
clearInterval(myInterval);
var aux = action.parentNode;
aux.removeChild(action);
}
function OnGoing() {
time++;
var hours = Math.floor(time / 100 / 60 / 60);
var minutes = Math.floor(time / 100 / 60 % 60);
var seconds = Math.floor(time / 100 % 60);
var hundreds = Math.floor(time / 10 % 10);
var thousands = time % 10;
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
timegraph.innerHTML = hours + ":" + minutes + ":" + seconds + ":" + hundreds + thousands;
}
function Running() {
if (ResetStart == 1) {
myInterval = setInterval(OnGoing, 10);
}
else {
timegraph.innerHTML = "00:00:00:00";
}
}
function Lap() {
var n = document.createElement("p");
n.setAttribute("id", "lappara");
var text = document.createTextNode(timegraph.innerHTML);
n.appendChild(text);
body.insertBefore(n, action);
}
但它仍然复制全文。 你可以帮我修改代码以缩短文本吗?
BR
答案 0 :(得分:1)
你正在缩短old_selection
,它没有像你想象的那样被使用。
您需要缩短selection
,但在之后将其复制到old_selection
,就像这样:
selection = window.getSelection();
var oldselection = selection;
selection = selection.toString().substr(0,500);
稍后,selection
用于填充新div,这就是为什么你需要缩短那个变量,而不是old_selection
。