【问题标题】:Jquery :data() selector not working, after being set with jquery使用 jquery 设置后,Jquery :data() 选择器不起作用
【发布时间】:2016-07-26 11:10:19
【问题描述】:

我的代码有错误,请注意[抱歉]

我希望至少匹配一个元素,但我得到零。
我做错了什么?

$("tr:first").data("aaa", "333"); // setting the data
console.log($("tr:data(aaa == "333")")); // reading the data

【问题讨论】:

  • 我不知道有这样的选择器。这可能来自插件吗?
  • @BoltClock api.jqueryui.com/data-selector 事情是……你是对的。我错过了.. 它是 jQueryUI。但是 - 它没有提醒我选择器在上下文中无效。 (我希望得到“错误:语法错误,无法识别的表达式:不支持的伪:数据”)
  • 抱歉 - 出现错误 - 已修复
  • 文档没有说明可以使用这样的选择器,似乎只支持$("tr:data(key)")

标签: jquery jquery-ui jquery-selectors html5-data


【解决方案1】:

改变这个:

console.log($("tr:data(aaa, 333)"));

仅限于此:

console.log($("tr:data(aaa)").data('aaa') === "333");

如果您要过滤trs,请使用.filter() 方法:

$("tr:data(aaa)").filter(function(i, el){
  return el.data('aaa') === "333"
}).css('color', 'red');

您实际上是在尝试将数据值再次设置为console log 中的tr 元素。

$('span').data('aaa', 3333);
var value = $('span:data(aaa)').data('aaa')
$('pre').html(value == 3333)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<span>3333</span>

<pre>pre</pre>

【讨论】:

  • 最好像console.log($("tr:data(aaa)") == "333"); // reading the data一样做
  • 这是一个条件,而不是选择器.. 帮不了我:\
  • 1.谢谢! 2. 我想要做的是迭代一些数字,并在每一轮中得到一个&lt;tr&gt;,它的data-index 与当前数字匹配。 index 值是使用 $(tr).data(index,number) 预设的
猜你喜欢
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-08
  • 2016-12-19
  • 2015-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多