鼠标悬停Javascript

时间:2017-10-10 10:02:53

标签: javascript

我有这个HTML:

function initHover () {
  var screen = document.getElementsById("iphoneScreen")

  screen.onmouseover = mouseOver
  screen.onmouseout = mouseOut
}


function mouseOver() {
  var welcomeMessage = document.getElementsByClassName("welcomeMessage")
  welcomeMessage.className += "screenOn"
}

和这个javascript:

    @Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();

    }

    void inject(MvmApp app);
}

我想在div上使用类“welcomeMessage”添加类“screenOn”,但它不起作用。愿有人帮帮我吗?

PS:我没有权利使用jQuery ......

1 个答案:

答案 0 :(得分:1)

我已经修复了你的js代码,所以尝试使用它知道,应该工作

JS:

function initHover () {
  var screen = document.getElementById("iphoneScreen");

   screen.onmouseover = mouseOver
   screen.onmouseout = mouseOut
}


function mouseOver() {
   var welcomeMessage = document.querySelector(".welcomeMessage");
   welcomeMessage.className += " screenOn";
}

mouseOver函数中注意我已将getElementsByClassName更改为querySelector,这样您就可以获得单个对象,而不是{{1}返回的实时HTMLCollection }}