0

我将数据分配到 $clicked_row:

$clicked_row.data("row", data);  

以下代码不起作用:

alert($clicked_row.hasData("row")); // want to have Boolean output 

但是,这有效:

alert($clicked_row.data("row")); // output the data 

为什么?

4

2 回答 2

1

你应该试试这个:

$.hasData($clicked_row);

此解决方案的来源:hasData 上的文档

For a short discussion on why hasData isn't a property of data objects, I refer you to this thread. In summary, you should stick to using .attr to determining if the element has a given attribute set, rather than just wanting to know if it has any attribute set. It will return undefined if the attribute isn't set.

于 2013-05-29T02:39:39.323 回答
0

失踪"

alert($clicked_row.hasData("行在这里));


或者你只是错误地分配数据。来自 jQuery 文档:

jQuery.data(document.body, 'foo', 52);
jQuery.data(document.body, 'bar', 'test');

jQuery.data(元素,键,值)

元素

  • 项目清单
  • 类型:元素
  • 与数据关联的 DOM 元素。

钥匙

  • 类型:字符串
  • 一个字符串,命名要设置的数据。

价值

  • 类型:对象
  • 新的数据值。

在你的情况下,你有elementkey但你错过了value

于 2013-05-29T02:35:11.447 回答