0

好的,我做了一些更改,修复了一些问题,但现在它并没有通过所有的字母,一些字母变得可见,然后脚本停止了。另外,我如何让字母淡入?我以为我必须这样做,但脚本由于某种原因没有暂停,而且你看不到褪色。

<script type="text/javascript" >

        //alert("Script Loaded");

        var $TotalLetters = 16,
        $SwappedLets = 0,
        $L1 = false,
        $L2 = false,
        $L3 = false,
        $L4 = false,
        $L5 = false,
        $L6 = false,
        $L7 = false,
        $L8 = false,
        $L9 = false,
        $L10 = false,
        $L11 = false,
        $L12 = false,
        $L13 = false,
        $L14 = false,
        $L15 = false,
        $L16 = false,
        $RanNum = 0,
        $Holderval = 0;

        window.onload = function DOL() {
            DoOnLoad()
        }

        function DoOnLoad() { 
            //alert("DoOnLoad");
            /* Change the opacity of every letter randomly,
            CAN CHANGE IN SMALL TAG */

            if ($SwappedLets <= $TotalLetters + 1) {
                $RanNum = Math.floor(Math.random()*17); 
                //alert(window["$L" + $RanNum]);
                 if (window["$L" + $RanNum] == false) {
                    window["$L" + $RanNum] = true;
                    OPIncre($RanNum);
                }
            }

        } 

        function OPIncre(RN) {
            if (document.getElementById("tL" + RN).style.opacity <= 1) {
                document.getElementById("tL" + RN).style.opacity = $Holderval + 0.001;
                $Holderval = $Holderval + 0.001;
                window.setTimeout(OPIncre(RN),600);
            }
            else {
                $SwappedLets = $SwappedLets + 1;
                $Holderval = 0;
                window.setTimeout(DoOnLoad,500);    
            }
        } 

    </script>

- - 老的:

我是 Javascript 的新手,我无法让脚本随机更改 html 元素的不透明度。默认情况下,元素的不透明度在 css 中设置为 0。我试过环顾四周,还没有找到解决办法。我在 Firefox 的 Javascript 错误控制台中没有收到任何错误。有什么明显的错误吗?

<script type="text/javascript" >
        /*alert("Script Loaded");*/
        var $TotalLetters = 16,
        $SwappedLets = 0,
        $L1 = false,
        $L2 = false,
        $L3 = false,
        $L4 = false,
        $L5 = false,
        $L6 = false,
        $L7 = false,
        $L8 = false,
        $L9 = false,
        $L10 = false,
        $L11 = false,
        $L12 = false,
        $L13 = false,
        $L14 = false,
        $L15 = false,
        $L16 = false,
        $RanNum = 0;

        window.onload = function DOL() {
            DoOnLoad()
        }

        function DoOnLoad() {
            /*alert("DoOnLoad");*/
            /* Change the opacity of every letter randomly,
            CAN CHANGE IN SMALL TAG */

            if ($SwappedLets <= $TotalLetters + 1) {
                $RanNum = Math.floor(Math.random()*17);
                /*alert(window["$L" + $RanNum]);*/
                if (window["$L" + $RanNum] == false) {
                    window["$L" + $RanNum] = true;
                    OPIncre($RanNum);
                }
            }

        }

        function OPIncre(RN) {
            while (document.getElementById("tL" + RN).style.Opacity <= 1) {
                document.getElementById("tL" + RN).style.Opacity = document.getElementById("tL" + RN).style.Opacity + 0.1;
            }
            $SwappedLets = $SwappedLets + 1;
            window.setTimeout(DoOnLoad,500);
        }
    </script>
4

1 回答 1

0

不确定“tL”是什么类型的元素,但假设它们是类型<p>,下面的脚本应该可以解决问题:

var $elements;
window.onload = function DOL() {
    // Get all elements
    $elements = $("p[id^='tL']");

    // Loop through all of them
    $elements.each(function(i, e) {
        e.style.opacity = Math.random() + 0.17;
    });
}

请注意,我在这里使用 jQuery。由于您对变量使用dollor 表示法,我假设您也在使用它。

于 2013-09-23T19:04:09.707 回答