2017-07-01 65 views
-3

我有一个HTML选择哪个触发电平变化的函数的run()一些JavaScript代码不起作用

<select id="select-para" onchange="run();"> 
    <option value="paragraph 1...">Para 1</option> 
    <option value="paragraph 2...">Para 2</option> 
    <option value="paragraph 3...">Para 3</option> 
    <option value="paragraph 4....">Para 4</option> 
</select> 

什么的run()的作用是,它设定的选择值等于可变文本和值的文本被设置为等于输入框的值。

function run(){ 
var text = document.getElementById("select-para").value; 

var storyTextarea = document.getElementById("storytext"); 
storyTextarea.value = text; 
} 

我正在打字测试,我希望用户选择他选择的段落。问题是,在这个函数结束之后,输入测试代码的其余部分不会触发。输入框中的段落改变,但输入测试代码的其余部分不起作用。如何使代码的其余部分工作?其余的代码在这里。

var textArr = text.split(" "); 
var usertext = ""; 
var lastWord = "" 
var usertextArr = new Array(); 
var mistakes = new Array(); 
var highlightArgs = new Array(); 
var score = 0; 
var count = 0; 
var highlightIndex = 0; 

//Prevent pasting into user text box 
$('textarea').bind("cut paste", function (e) { 
    e.preventDefault(); 
}); 

//Keep highlighted text responsive 
$(window).on('resize', function(){ 
    $(".highlightTextarea").css('width','100%'); 
    $(".highlightTextarea-container").css('width','99%'); 

    if (highlightArgs.length > 0){ 
     updateHighlight(); 
    } 
}); 

//Jump to next word to be typed 
function textJump(jumpIndex){ 
    var textStr = text.substring(0, jumpIndex); 
    storyTextarea.value = textStr; 
    storyTextarea.scrollTop = storyTextarea.scrollHeight; 
    storyTextarea.value = text; 
} 

//Jump to specified line of TextArea 
//OLD METHOD DO NOT USE 
function textareaJump(line){ 
    storyTextarea = document.getElementById("storytext"); 
    var lht = (storyTextarea.clientHeight/storyTextarea.rows)*0.875; 
    var jump = (line - 1) * lht; 
    storyTextarea.scrollTop = jump; 
} 

//Refresh the highlighted area 
function updateHighlight(){ 
    $('#storytext').highlightTextarea('destroy'); 
    $('#storytext').highlightTextarea({ ranges: highlightArgs }); 
} 

function typeTest(){ 

    function updateUsertext(){ 
    usertext = $('textarea#usertext').val(); 
    var usertextLatestArr = usertext.split(" "); 
    usertextArr.push(usertextLatestArr[usertextLatestArr.length-1]); 
    count = usertextArr.length - 1; 
    var wordLen = textArr[count].length; 
    var charBufferIndex = textArr[count].length < 3 ? 5 : 2; 

    //Word spelling matches 
    if(textArr[count] === usertextArr[count]){ 
     if (mistakes[mistakes.length-1] === count){ mistakes.pop() } 
     score++; 
     highlightArgs.push({ color: '#c1f5b0', start: highlightIndex, length: wordLen }) 
     highlightIndex += (wordLen + 1); 
    } 

    //Missed one word 
    //any more than a single consecutive missed word counts as an error 
    else if(textArr[count+1] === usertextArr[count]){ 
     usertextArr.splice(count, 0, "blank"); 
     if (mistakes[mistakes.length-1] === count){ mistakes.pop() } 
     score++; 
     mistakes.push(count); 
     highlightArgs.push({ color: '#febbb9', start: highlightIndex, length: wordLen }) 
     highlightIndex += (wordLen + 1); 
     highlightArgs.push({ color: '#c1f5b0', start: highlightIndex, length: textArr[count+1].length }) 
     highlightIndex += (textArr[count+1].length + 1); 
    } 

    //Spelling mistake 
    else{ 
     highlightArgs.push({ color: '#febbb9', start: highlightIndex, length: wordLen }) 
     highlightIndex += (wordLen + 1); 
     mistakes.push(count); 
    } 

    //Rebuild the highlight object 
    updateHighlight(); 

    //Jump to the next word 
    var jumpIndex = highlightIndex + (wordLen + 1) + charBufferIndex; 
    textJump(jumpIndex); 
    }; 

    //User presses backspace 
    $('#usertext').on('keydown', function(e) { 
    var lastChar = $('textarea#usertext').val().slice(-1); 
    var secondLastChar = $('textarea#usertext').val().slice(-2).substring(0, 1);; 
    if(e.keyCode == 8 && lastChar === " " && secondLastChar !== " "){ 
     usertextArr.pop(); 
     mistakes.pop(); 
     highlightArgs.pop(); 
     updateHighlight(); 
     highlightIndex -= (textArr[count].length + 1); 
     count--; 
    } 
    }); 

    $('#usertext').on('keydown', function(e) { 
    var lastChar = $('textarea#usertext').val().slice(-1); 
    var spaceTest = lastChar === " " ? true : false; 
    if(e.keyCode == 32 && spaceTest == false){ 
     updateUsertext(); 
    } 
    }); 
} 

如何使所有的代码工作和打字测试功能的顺利进行。下面是HTML:

<select id="select-para" onchange="run();"> 
     <option value="paragraph 1...">Para 1</option> 
     <option value="paragraph 2...">Para 2</option> 
     <option value="paragraph 3...">Para 3</option> 
     <option value="paragraph 4....">Para 4</option> 
    </select> 
<div class="typebox"> 
      <textarea id="storytext" name="storytext" class="form-control" type="text" readonly="readonly" rows="3"></textarea> 
     </div> 

     <div class="typebox"> 
      <textarea id="usertext" name="usertext" type="text" class="form-control" rows="2" placeholder="Start typing here to begin the test..."></textarea> 
     </div> 

     <div class="timer">You have <span id="time" class="timerTime">02:00</span> minutes left.</div> 
+0

从哪里调用typeTest()方法?你声明run()方法被调用后,该方法正在工作,但正在发生?你的控制台是否出现错误? – javaBean007

+0

这是一个“为我解决所有问题”,因此太宽泛了。你能缩小它吗? – halfer

回答

0

添加typeTest()同时段的onChange

<select id="select-para" onchange="run(); typeTest();"> 

包括

$("#usertext").bind("cut paste", function (e) { 
    e.preventDefault(); 
}); 

//Keep highlighted text responsive 
$(window).on('resize', function(){ 
    $(".highlightTextarea").css('width','100%'); 
    $(".highlightTextarea-container").css('width','99%'); 

    if (highlightArgs.length > 0){ 
     updateHighlight(); 
    } 
}); 

在run()函数,使处理器获得注册。

从中删除“textarea”,因为它不需要标识ID。

$("textarea#usertext") 

该逻辑可能需要正确格式化以适用于除'8'bs和'32'空间以外的所有其他键码。

+0

先生,代码工作正常,如果我在javascript中提供段落,但添加下拉后,段落在文本框中更改,但计时器不启动。创建另一个函数typetest()来触发代码的其余部分也不会使其工作。请帮助我,我从两天卡住了。 – user8235202

+0

计时器不是从你的代码开始的。每次更改段落时,都需要启动它。您正在定义typeTest()函数,除非您从HTML调用它,否则它不会被触发。所以删除typeTest()函数,以便jQuery调用从脚本自动注册。 –