Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.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/75.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 如何查找具有数据属性的标记_Javascript_Jquery_Html_Custom Data Attribute - Fatal编程技术网

Javascript 如何查找具有数据属性的标记

Javascript 如何查找具有数据属性的标记,javascript,jquery,html,custom-data-attribute,Javascript,Jquery,Html,Custom Data Attribute,我正在尝试查找具有数据方向属性的标记。不知何故,它总是未定义的。我要做的是获取数据方向和数据cellId的值。 有什么想法吗 var row = $('<tr data-cellId="' + cell.fatherId + '" data-direction="' + cell.direction + '"/>'); var nameColumn = $('<td class="port-name-col" data-cellId="' + cell.fatherId + '

我正在尝试查找具有
数据方向
属性的标记。不知何故,它总是未定义的。我要做的是获取
数据方向
数据cellId
的值。 有什么想法吗

var row = $('<tr data-cellId="' + cell.fatherId + '" data-direction="' + cell.direction + '"/>');
var nameColumn = $('<td class="port-name-col" data-cellId="' + cell.fatherId + '">' +cell.value.name + '</td>');
var btnColumn = $('<td class="port-btn-col"><button type="button" title="Remove" class="btn btn-default btn-xs x-button-icon btn-delete-port"><span class="glyphicon glyphicon-trash"></span></button></td>');
row.append(nameColumn);
row.append(btnColumn);

问题在于这一行:

var tr = $(elem).find("tr.data-cellId");
.data cellId
部分基于具有该类的元素进行选择。要查找属性,请在选择器中使用
[data cellId]

var tr = $(elem).find("tr[data-cellId]");
然后,要获取该属性的值,可以使用:

tr.attr('data-cellId');

尝试使用这个
$(elem).find(tr[datacellid])
您是否在任何时候将
附加到DOM?嗯,看起来我的问题问错了。我在检查警报(tr.html())时仍然没有定义。问题是,“tr[data cellId]”不是$(elem)的子元素。但令人失望的是,我需要找到带有data-cellId的tr标记。您能在jsfiddle.net示例中显示代码的一个工作(或者在您的例子中,几乎工作)示例吗?如果
tr
不是$elem的孩子,为什么还要在上面使用
find()
?也许你可以试试
var tr=$('tr[data cellId]')
tr.attr('data-cellId');