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
cakephp-2.1 - Cakephp 使用返回的数组值
1
facebook-graph-api - 删除页面选项卡
0
eclipse - 将 perforce 项目从插件导入 eclipse
2
matlab - 如何在 MATLAB 中将 '1*1 char' 转换为逻辑向量?
1
objective-c - Google Drive API - 目标 C - 示例?
1
qt - QTableWidget cellClicked 信号不起作用
1
python - 在其他产品中为开发人员和用户分发 python 助手应用程序
2
html - 在 IE 中打印带有分页符的表格
4
python - 使用 scrapyd 部署项目时出错
1
java - 从生产者到消费者的可靠文件传递

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