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
jquery - JQuery 属性和字符串
3
iphone - 使用 MVC 的 iOS 多视图注册页面
1
c# - 如何在 c# webbrowser 控件中检查连接
1
drupal - 将模块应用于所有过去的文章
3
string - 在 VBA 中搜索包含小数的数字的字符串
3
python - python:使用正则表达式从日志文件中读取日期时间
1
ios - NSJSONSerialization - 核心数据与 JSON 的关系
4
php - 如何调用数据库的一个单元格?
4
android - 循环动画集
2
javascript - 当 tbody 不存在时附加到表以及如何使所有现有的 jquery 为该行工作

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