简体   繁体   English

为什么返回未定义? jQuery的

[英]Why is this returning undefined? jquery

I have this line <table id='<?= $value['Name']?>'> in my PHP which just sets an ID which I can target. 我在PHP中有这行<table id='<?= $value['Name']?>'> ,它只是设置了一个我可以定位的ID。

This table is inside a <div> with id="god" . 此表位于id="god"<div>内部。

But when i click the table which has this script: 但是,当我单击具有此脚本的表时:

$("#god table").click(function(){
    var link = $(this).id;
    alert(link);
});

It alerts undefined - could you tell me why that is? 它会发出undefined警报-您能告诉我为什么吗?

My best guess would be that it targets the <td> which I click on for the $(this) but I am not sure - and I do not know how to test that. 我最好的猜测是,它以我点击$(this)<td>为目标,但我不确定-我也不知道如何测试。

Use the following: 使用以下内容:

var link = this.id;

The jQuery object $(this) does not have a propery id . jQuery对象$(this)没有属性id

Note: DO NOT use $(this).attr('id') when you can use this.id which is way more efficient. 注意:当您可以使用this.id时,请不要使用$(this).attr('id') ,这样会更有效。 also, note that id is case sensitive so be consistent with "God" and "god". 另外,请注意, id区分大小写,因此请与“上帝”和“上帝”保持一致。

   var link =  $(this).id;

Supposed to be 应该是

either 要么

   var link =  $(this).attr('id');

or 要么

   var link =  this.id;

$(this) is a jQuery object. $(this)是一个jQuery对象。 And it does not have the .id property 而且它没有.id属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM