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
php - 如何修复成功上传文件的 503 错误?
2
perl - 如何将输入从脚本传递到命令行
3
java - 在eclipse Android应用程序中Java调试Java
2
c - 我是否覆盖了我的链接列表?
2
iphone - iOS - 使用键盘“下一步”按钮更改 ViewController?
3
java - 使用事件处理程序更改对象参数
1
iis-7.5 - IIS URL 重写模块取消编码查询字符串
1
python - Twisted web - 在响应客户端后保留请求数据
1
prolog - Swi-Prolog:无权修改静态过程
1
c# - 如何在一天或一周或一个月或一年之间查询 Gridview 中的数据

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 知识共享许可协议。