如何在jquery中console.log数据属性的值? [英] How to console.log value of a data-attribute in jquery?

查看:398
本文介绍了如何在jquery中console.log数据属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此console.log部分只是为了使我更容易在将这段代码实现到整体解决方案中之前查看输出.我在堆栈溢出中读到,您是通过以下方式选择数据属性的:

So the console.log part is just to make it easier for me to see the output before I implement this piece of code into my overall solution. I read on stack overflow that you select the data attribute in this way:

$("li[data-city*=New York]")

但是说,例如,我想选择我的html中的所有数据属性,如下所示:

But say for instance I want to select all my data attributes which look like this in my html:

 data-group='value'

因此,一旦我选择了这些数据属性,我将如何console.log记录数据组属性的实际字符串值部分.

So then once I have selected these data attributes, how would I console.log the actual string value part of the data-group attribute.

这似乎是一个基本问题,但即使是简单的简短回答也将大有帮助.问候:)

It may seem like a basic question but a even a simple short answer would help greatly. Regards :)

推荐答案

要选择所有具有data-group属性的元素,您可以遍历选择器[data-group]选择的元素,并在迭代中获得使用 data 方法的属性值.

To select all the elements which have data-group attribute, you can loop over the elements selected by the selector [data-group], and in the iteration, get the value of the attribute using the data method.

请参见以下示例:

$('[data-group]').each(function() {
  console.log($(this).data('group'));
})

这篇关于如何在jquery中console.log数据属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆