首页 > 解决方案 > 使用JS将鼠标悬停在文本上时更改正文背景图像

问题描述

基本上,当鼠标经过文本时,我正在尝试使用 JS 更改正文图像,在我向您展示的代码中,我现在使用一条线来更改正文颜色,但我不知道使它更改图像的语法,所以我试图为图像写一行,我评论了它,因为我不知道它是否正确,这是当鼠标悬停时工作改变颜色的代码。

    <div class="row">
        <div class="col-xl-4 offset-xl-4 text-center">
            <p onmouseover="return bg1();"><a href="#" type="onmouseover" class="dimen">Sfondo 1</a></p>
            <p onmouseover="return bg2();"><a href="#" type="onmouseover" class="dimen">Sfondo 2</a></p>
            <p onmouseover="return bg3();"><a href="#" type="onmouseover" class="dimen">Sfondo 3</a></p>
            <p onmouseover="return bg4();"><a href="#" type="onmouseover" class="dimen">Sfondo 4</a></p>



        </div>
    </div>

</body>

<script>
    //  document.getElementById("bgchange").style.backgroundImage= url(../media/battimani.jpg);


    function bg1(){

        document.getElementById("bgchange").style.backgroundColor = "#66ff66";

    }

    function bg2(){

        document.getElementById("bgchange").style.backgroundColor = "#eb4034";

    }
    function bg3(){

        document.getElementById("bgchange").style.backgroundColor = "#ebde34";

    }

    function bg4(){

        document.getElementById("bgchange").style.backgroundColor = "#3440eb";

    }

</script>

只复制了正文,你能告诉我如何正确编写脚本中的第一行注释吗?我想以 4 种不同的方式使用它,比如 4 种不同的颜色,谢谢 =)

ps:一周前刚开始学js,所以我的水平是basic basic

标签: javascripthtmlbackground

解决方案


尝试这个:

  document.getElementById("bgchange").style.backgroundImage = "url(../media/battimani.jpg)";

推荐阅读