0

I want to read a normal textfile line-by-line. Each line should be copied to the clipboard (Internet Explorer), and when the user pastes the clipboard (into an input field) the next line should be copied to his clipboard.

My code looks now like this:

<script>
$.get('list.txt', function(data) {
    var lines = data.split('\n');
    for (var i = 0, len = lines.length; i < len; i++) {
        window.clipboardData.setData('Text', lines[i]);
    }
});

$('#text').bind("paste", function(e) {
    $("#output").html(window.clipboardData.getData('Text'));
});
</script>

My problem is I don't know how to stop the loop until the user pastes his clipboard, and then to continue.

4

1 回答 1

0

您需要在listenerfor the paste 事件中增加循环,而不是一次性循环所有内容。

(function () { // scope closure
    var lines = [], i = -1; // initial `i` -1 so `i + 1 === 0` => first item

    function setClipboard() { // modify to next item in `lines`
        i = (i + 1) % lines.length; // make `i` go back to 0 if at end
        window.clipboardData.setData('Text', lines[i]);
    }

    $.get('list.txt', function(data) {
        lines = data.split('\n'); // give lines it's data
        setClipboard(); // initial
    });

    $('#text').bind("paste", function(e) {
        $("#output").html(window.clipboardData.getData('Text'));
        setClipboard(); // change to next line
    });
}());
于 2013-06-25T10:05:48.597 回答