Stack Overflow
  1. 首页
  2. 工具
  3. 网站转让
  1. 首页
    1. 导航
    2. 问题
    3. 标签
    1. Teams
      Stack Overflow for Teams – Start collaborating and sharing organizational knowledge. Create a free Team Why Teams?
    2. Teams
    3. Create free Team
Collectives on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more

javascript - Javascript/jQuery:为什么 event.target 使用标签的结果不同。console.log 中的其他元素,alert() 考虑到 $(this)

翻译自:https://stackoverflow.com/questions/18561700 2013-09-01T18:47:35.940
423 次
This question shows research effort; it is useful and clear
1
This question does not show any research effort; it is unclear or not useful
Bookmark this question.
Show activity on this post.

在使用 event.log 处理 DOM 时,似乎有一些值得理解的东西。

比较警报中的结果和 firebug 中的 console.log。

我有这个代码 HTML:

<div>Click Me < Div ></div>
<a href="#">Click Me < a ></a> 

和JS:

$(function(){
    $('div').click(function(e){

        console.log(e.target);
        console.log($(this));
        alert(e.target)
        alert($(this))
    })

    $('a').click(function(e){
       e.preventDefault();

        console.log(e.target);
        console.log($(this));

        alert(e.target)
        alert($(this))
    })
})

当 Div 或被点击时:

这console.log都按预期工作

但是alerts 出现不同的输出:

对于 DIV:

1 objectHTMLDivElement
2 object 对象//using jQuery's $(this)

但是,当<a>标签被点击时,它会产生:

1 网址或href属性值

2 对象对象//using jQuery's $(this)

为什么会这样?为什么不<a>将此标签打印为 HTMLElement 的警报?

请在这里自己测试:http: //jsfiddle.net/hWR53/1/

javascriptjqueryhtmlevents
4

1 回答 1

This answer is useful
2

所有对象,其中包括元素,都有一个toString函数,并且toString在对象上调用此函数以构建警报中显示的内容。

对于大多数对象,此函数返回"[object Object]",但元素的实例a将其覆盖以返回href元素属性的值。

于 2013-09-01T18:49:43.557 回答

Related

1
asp.net-mvc - MVC 路由寄存器
3
java - 使用 JPA 进行多选
1
lua - Lua 中用于移动应用程序的计时器功能 - “已关闭”
1
ember.js - 如何使用 Zendesk 的 ember-resource 适配器的示例
2
c - 在函数指针代码中显示错误
2
android - sqlite中的按位异或 - 按位不按我预期的那样工作
2
c# - 试图获得一个整体
在 c# 中使用正则表达式
1
python - 动态创建具有继承和类方法的类列表
1
regex - 转义加号不起作用
3
powershell - Powershell 或批处理 EXITCODE 7zip

Reference

php × 1429865
c/c++ × 756500
nginx × 49975
mongodb × 159057
mybatis × 3233
anaconda × 13410
pycharm × 14671
python × 1902243
vscode × 56040
docker × 110988
github × 49000
flask × 49129
ffmpeg × 24037
jmeter × 16910
matplotlib × 63493
bootstrap × 54641
Stack Overflow 中文网

遵从 CC BY-SA 知识共享许可协议。