Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 返回一个html表格元素';s id,并解析该id_Javascript_Jquery_Html - Fatal编程技术网

Javascript 返回一个html表格元素';s id,并解析该id

Javascript 返回一个html表格元素';s id,并解析该id,javascript,jquery,html,Javascript,Jquery,Html,我有一个元素表,我希望能够单击表中的一个正方形。当我点击方块时。每个方块都是一个“td”元素,它有一个唯一的id。我想在单击它时返回id的id,然后我想用另一个函数解析id。这是我正在尝试的,但它不起作用 td.onclick=function(){ return td.id} 然而,我有点奇怪,这句话确实有效 td.onclick=function(){ alert(td.id)} 另外,假设我能够返回td.id,我不知道如何将它传递给另一个

我有一个元素表,我希望能够单击表中的一个正方形。当我点击方块时。每个方块都是一个“td”元素,它有一个唯一的id。我想在单击它时返回id的id,然后我想用另一个函数解析id。这是我正在尝试的,但它不起作用

td.onclick=function(){
          return td.id}
然而,我有点奇怪,这句话确实有效

td.onclick=function(){
              alert(td.id)}

另外,假设我能够返回td.id,我不知道如何将它传递给另一个函数来解析它。非常感谢您的帮助

只需在onclick中调用另一个函数

td.onclick = function(){
   var eleID = this.id;
   someOtherFunction(eleID);
};

function someOtherFunction(id){
   //the element's id will be in the variable id
}
由于您使用的是jQuery,所以您可以

$("#myTableId td").click(function(){
   var eleID = this.id; //or $(this).id but no sense to wrap it to just get an id.
   someOtherFunction(eleID);
});
而不是单独设置每个td元素的onclick属性

$("#table1 tr td").each(function(i,td) { $(td).click(function() { if($(this).attr("id")!=null) { alert($(this).attr("id")); } }); }); $(“表1 tr td”)。每个(功能(i,td) { $(td)。单击(函数() { if($(this.attr(“id”)!=null) { 警报($(this.attr(“id”)); } }); });
首先,想想你要把字符串发送到哪里。在
onClick
函数中调用函数

而不是

td.onclick=function(){
          return td.id}
返回
td.id
,使用
this.id

td.onclick=function(){
              functionToCall(this.id)}

你能给我们展示一下你在a里有什么吗?@Jeanluca Scaljeri我会试着做一个JSFIDLE。我以前从未这样做过。在这里我测试了一个网站,这个jQuery解决方案非常完美。