jQuery - 在Div2上显示Div1单击,然后在文档单击上隐藏Div1

时间:2017-07-12 15:22:52

标签: jquery hide show

我有一个简单的问题。我正在尝试在用户点击div时显示视频。然后,当用户点击页面上不在视频上的任何位置时,隐藏该视频。完成了数百万次,但我无法找到如何做到这一点,Stackoverflow上的其他部分解决方案失败了。 这是完整的代码:

{
  "name": "angular-quickstart",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "build": "tsc -p src/",
    "build:watch": "tsc -p src/ -w",
    "build:e2e": "tsc -p e2e/",
    "serve": "lite-server -c=bs-config.json",
    "serve:e2e": "lite-server -c=bs-config.e2e.json",
    "prestart": "npm run build",
    "start": "concurrently \"npm run build:watch\" \"npm run serve\"",
    "pree2e": "npm run build:e2e",
    "e2e": "concurrently \"npm run serve:e2e\" \"npm run protractor\" --kill-others --success first",
    "preprotractor": "webdriver-manager update",
    "protractor": "protractor protractor.config.js",
    "pretest": "npm run build",
    "test": "concurrently \"npm run build:watch\" \"karma start karma.conf.js\"",
    "pretest:once": "npm run build",
    "test:once": "karma start karma.conf.js --single-run",
    "lint": "tslint ./src/**/*.ts -t verbose"
  },
  "keywords": [],
  "author": "",
  "license": "MIT",
  "dependencies": {
    "@angular/animations": "^4.2.6",
    "@angular/cdk": "github:angular/cdk-builds",
    "@angular/common": "~4.0.0",
    "@angular/compiler": "~4.0.0",
    "@angular/core": "~4.0.0",
    "@angular/forms": "~4.0.0",
    "@angular/http": "~4.0.0",
    "@angular/material": "github:angular/material2-builds",
    "@angular/platform-browser": "~4.0.0",
    "@angular/platform-browser-dynamic": "~4.0.0",
    "@angular/router": "~4.0.0",
    "angular-in-memory-web-api": "~0.3.0",
    "core-js": "^2.4.1",
    "hammerjs": "^2.0.8",
    "rxjs": "5.0.1",
    "systemjs": "0.19.40",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@types/jasmine": "2.5.36",
    "@types/node": "^6.0.81",
    "canonical-path": "0.0.2",
    "concurrently": "^3.5.0",
    "jasmine-core": "~2.4.1",
    "karma": "^1.3.0",
    "karma-chrome-launcher": "^2.2.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-jasmine-html-reporter": "^0.2.2",
    "lite-server": "^2.2.2",
    "lodash": "^4.16.4",
    "protractor": "~4.0.14",
    "rimraf": "^2.5.4",
    "tslint": "^3.15.1",
    "typescript": "~2.2.2"
  },
  "repository": {}
}

当我点击“.clickForVideo”div时,不会显示任何视频。如果我注释掉标记为“参考1”的行,然后单击“.clickForVideo”,则会出现视频/ div,但是,如果按预期单击文档,则不会消失。 如何更改我的代码或为此问题提供不同的解决方案。 非常感谢。 安德鲁

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为 clickForVideo 也是文档的一部分。当您单击它时,文档(父级)的单击事件也会被调用,并再次隐藏它。

试试这个:

$(function(){
    $(".clickForVideo").click(function(event) {
        event.stopPropagation(); //<-----
        $("#videoDiv").show();
    });
    $(document).click(function(event){
        if ($('#videoDiv').is(':visible')) {
          if(!$(event.target).closest('#videodiv').length) {
            $('#videoDiv').hide();
          }
        }
    });
});

event.stopPropagation()DOC)应该阻止事件冒泡到父级,即document