父元素<a>标签的默认行为以及click事件之间的相互影响


本文摘自PHP中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

开发过程中遇到问题,简单写个demo 运行环境为Chrome 68

描述一下这个问题,当a标签内部存在嵌套时, 父元素a标签的href默认行为以及子元素绑定的click事件的响应之间存在影响。页面结构:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

<!DOCTYPE html>

<html lang="en">

 

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <meta http-equiv="X-UA-Compatible" content="ie=edge">

    <title>a标签内部点击事件失效</title>

    <style>

        * {

            margin: 0;

            padding: 0;

        }

 

        .father {

            display: block;

            width: 500px;

            height: 200px;

            background-color: rgb(210, 111, 30);

        }

 

        .child-one {

            display: block;

            width: 200px;

            height: 50px;

            background-color: rgb(174, 43, 226);

        }

 

        .child-two {

            display: block;

            width: 200px;

            height: 50px;

            background-color: rgb(43, 226, 67);

        }

 

        .child-three {

            display: block;

            width: 200px;

            height: 50px;

            background-color: rgb(43, 137, 226);

        }

    </style>

</head>

 

<body>

    <a class="father" href="father" onclick="alert(111)">父标签

        <span class="child-one">

            子标签1

        </span>

        <object>

            <a class="child-two" href="child-two">

                子标签2

            </a>

        </object>

        <object>

            <a class="child-three" href="javascript:alert('href:child-three')">

                子标签3

            </a>

        </object>

    </a>

    <script>   

        let father = document.querySelector('.father');

        let ele1 = document.querySelector('.child-one');

        let ele2 = document.querySelector('.child-two');

        let ele3 = document.querySelector('.child-three');

 

        ele1.addEventListener('click', function (e) {

            e.stopPropagation();

            // e.preventDefault();

            alert('click child-one')

            window.location.href = 'child-one'

        }, false)

 

        ele2.addEventListener('click', function (e) {

            e.stopPropagation();

            alert('click child-two')

            // window.location.href='child-two'

        }, false)

 

        ele3.addEventListener('click', function (e) {

            alert('click child-three')

            window.location.href = 'child-three'

        }, false)

 

        father.addEventListener('click', function (e) {

            alert('click father')

            window.location.href = 'father'

        }, false)

 

    </script>

</body>

 

</html>

示例如下图(如果a标签嵌套,浏览器解析错误,所以用object标签包裹了一层)。

1851056028-5b6980c476a7c_articlex.jpg

执行操作:

  1. 当点击父标签时,先弹出111,然后跳转父标签的href链接。
    说明onclick执行先于href

  2. 当点击child-one时,执行元素绑定的click事件,会弹出alert,但是location仍然跳转到了father。
    阻止冒泡后,执行结果仍然不符合预期。添加preventDefault之后,执行了子元素自己的跳转。

  3. 当点击child-two时,弹出响应信息,然后会跳转href的链接。

  4. 当点击child-three时,先弹出click child-three,然后是href child-three,说明click事件先于href执行。

上面4个操作除了2之外都很好理解,2中,为什么已经在阻止了事件冒泡之后,仍然执行了父元素中href的跳转。

思考:

首先可以肯定的是,事件冒泡确实被阻止了,因为父元素的onclick并没有执行。
所以猜测,<a>标签的默认行为是无法通过取消冒泡来阻止的,就算事件没有冒泡到父元素,子元素在父元素<a>标签内部,仍然会执行<a>标签默认行为。

解决方法:

在子元素中添加e.preventDefault()阻止默认行为

父元素不使用<a>标签,使用其他标签绑定click事件且子元素阻止冒泡

父元素不使用href属性,直接在<a>标签上绑定click事件

相关文章推荐:

link标签链接CSS和@import加载有什么区别?

HTML标签:img标签的用法总结

以上就是父元素<a>标签的默认行为以及click事件之间的相互影响的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript引用类型的详细介绍(附示例)

javascript如何将字符串转为数字

html中定义多个class属性无效

javascript 设计模式之单例模式

javascript的匿名方法是什么

javascript中bind是什么意思

javascript是前端吗

html怎么引用css

html的由来

javascript检测出当前浏览器是否是无头浏览器

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...