JS使字符串右击移动

首次发布:2017-08-20 22:23
    <script type="text/javascript">
        var leftOrLight = 'left';
        window.onload = function () {
            setInterval(function () {
                //var titleValue = document.title;//获取标题这里没有用上
                var testp1 = document.getElementById('p1').value;
                if (leftOrLight == 'left') {
                    var fistValue = testp1.charAt(0);
                    var others = testp1.substring(1, testp1.length);
                    document.getElementById('p1').value = others + fistValue;
                } else {
                    var lastValue = testp1.charAt(testp1.length - 1);
                    var others = testp1.substring(0, testp1.length - 1);
                    document.getElementById('p1').value = lastValue + others;
                }
                
            }, 1000)
            document.getElementById('left').onclick = function () {
                leftOrLight = 'left';
            }
            document.getElementById('right').onclick = function () {
                leftOrLight = 'right';
            }
        };
    </script>

    <input id="left" type="button" value="left" />
    <input id="right" type="button" value="right" />
    <input id="p1" type="text" value="123456789" />