如何为同一元素上的同一事件调用/处理不同的事件处理程序?的JavaScript

时间:2018-12-24 21:09:28

标签: javascript html dom

我很好奇,我知道如果我使用称为DOM Level 2()的addEventListener方法,可以将多个事件处理程序附加到同一元素,所以我想知道是否有这种情况:

someButton.addEventListener("mouseover", showHint, false);

someButton.addEventListener("mouseover", showHintTwo, false);

在现实世界中这可能吗?上面的示例

我该如何选择何时要调用showHint和何时要调用showHintTwo,是否有可能以某种方式处理/分离呢……因为两个事件处理程序都会mouseover发生时开火?

谢谢大家

欢呼

2 个答案:

答案 0 :(得分:1)

喜欢吗? https://codepen.io/cyruscuenca/pen/xmrxob

这没有什么意义,但是可以满足您的需求。

  • 2个事件处理程序。
  • 您可以选择使用切换开关实际显示到哪个块。

我将亲自添加一个事件处理程序,并传递如下条件:

Promise.resolve(gapi.auth2.getAuthInstance().signIn()).then

答案 1 :(得分:0)

处理此问题的一种好方法是创建一个单独的提示计数器变量,然后显示您选择的相关提示。您想如何更新计数器将决定何时显示哪个提示。

# This file is a template, and might need editing before it works on your project.
# Read more about this script on this blog post https://about.gitlab.com/2018/10/24/setting-up-gitlab-ci-for-android-projects/, by Jason Lenny
image: openjdk:8-jdk

variables:
  ANDROID_COMPILE_SDK: "28"
  ANDROID_BUILD_TOOLS: "28.0.2"
  ANDROID_SDK_TOOLS: "4333796"

cache:
  untracked: false
  key: ${CI_PROJECT_ID}
  paths:
    - android-sdk-linux
    - .m2/
    - .gradle/
    - android-sdk.zip
    - /var/cache/apt/
    - android.keystore
    - keystore.properties

stages:
  - init
  - test
  - build

downloadAndroidSDK:
  stage: init
  script:
    - KS_PASS=$(date +%s | sha256sum | base64 | head -c 32 ; echo)
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - apt-get --quiet update --yes
    - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
    - wget --quiet --continue --output-document=android-sdk.zip https://dl.google.com/android/repository/sdk-tools-linux-${ANDROID_SDK_TOOLS}.zip
    - unzip -o -d android-sdk-linux android-sdk.zip
    - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_COMPILE_SDK}" >/dev/null
    - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" >/dev/null
    - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" >/dev/null
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    # temporarily disable checking for EPIPE error and use yes to accept all licenses
    - set +o pipefail
    - yes | android-sdk-linux/tools/bin/sdkmanager --licenses
    - set -o pipefail
    - rm -f $CI_PROJECT_DIR/android.keystore
    - echo y | keytool -genkeypair -keyalg RSA -ext KeyUsage:critical=digitalSignature -ext ExtendedkeyUsage:critical=codeSigning -dname "ou=Wdes, c=FR" -alias wdes -keypass $KS_PASS -keystore $CI_PROJECT_DIR/android.keystore -storepass $KS_PASS -validity 1 -deststoretype pkcs12
    - keytool -list -storepass $KS_PASS -v -keystore $CI_PROJECT_DIR/android.keystore
    - echo -e "release.key.alias=wdes\nrelease.key.password=$KS_PASS\nrelease.storeFile=$CI_PROJECT_DIR/android.keystore\nrelease.password=$KS_PASS\n\ndebug.key.alias=wdes\ndebug.key.password=$KS_PASS\ndebug.storeFile=$CI_PROJECT_DIR/android.keystore\ndebug.password=$KS_PASS\n" > keystore.properties

assembleDebug:
  stage: build
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

assembleRelease:
  stage: build
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew assembleRelease
  artifacts:
    paths:
    - app/build/outputs/

lintDebug:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew -Pci --console=plain :app:lintDebug -PbuildDir=lint

lintFiles:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew ktlint

debugTests:
  stage: test
  dependencies: [ 'downloadAndroidSDK' ]
  script:
    - export GRADLE_USER_HOME=$(pwd)/.gradle
    - export ANDROID_HOME=$PWD/android-sdk-linux
    - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
    - chmod +x ./gradlew
    - ./gradlew -Pci --console=plain :app:testDebug

如果需要设置第二个提示以在一定时间后显示,请查看JS中的let hintCounter = 1; function showHint() { switch(hintCounter) { case 1: // show first hint case 2: // show second hint } hintCounter++; } someButton.addEventListener("mouseover", showHint, false); 函数。