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
c++ - ifstream 读取二进制数据发出 0x00 字节
1
lwuit - 如何释放已用 Connectionrequest 的内存
2
ruby-on-rails - RMagick 在 Windows 上安装 Redmine 时出错
2
c++ - 关于linux内存问题的C++多线程程序
1
jquery - 带有 json 的 Jquerypopup
1
python - python pyqt和父类
1
windows-phone-7 - 如何在 windows phone 7 中为应用程序栏添加背景图像?
1
io - 每个进程都有自己的标准输入、标准输出和标准错误吗?
1
ios - 为什么即使配置文件设置为分发,xcode 也会抛出此错误?
2
java - 休眠(lucene)搜索中的和运算符

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