使用javascript从锚标记获取自定义数据

时间:2022-11-27 17:55:52

I'm trying to obtain this value for a javscript function

我正在尝试为javscript函数获取此值

<a href="#" id="like" name="like" data-value="<?php echo $_GET['id'];?> " onclick="like(); return false;">
                <img src="<?php echo $ROOT. "/assets/images/templates/like.png"?>" class="aligncenter" alt="" /></a>

I'd like to get the id which is the data-value.

我想得到id是数据值。

I've tried using jquery selectors such as var id = $('a[data-value]').attr();but obviously have it wrong.

我已经尝试过使用jquery选择器,例如var id = $('a [data-value]')。attr();但显然有错误。

Any help is appreciated.

任何帮助表示赞赏。

4 个解决方案

#1


3  

Plain JavaScript has 2 Variants: getAttribute() and dataset:

普通JavaScript有2个变体:getAttribute()和数据集:

var el = document.getElementById( 'like' );
el.getAttribute( 'data-value' ); 
el.dataset.value;

As you tagged this with jQuery as well, you could use data() or attr():

当你用jQuery标记它时,你可以使用data()或attr():

$( '#like' ).data( 'value' );
$( '#like' ).attr( 'data-value' );

Using data() you can only read the value, but not update it! data() refers to a jQuery data object, which is just initialized with data- fields of the HTML element, but does not update them!

使用data()只能读取值,但不能更新它! data()指的是一个jQuery数据对象,它只是用HTML元素的数据字段初始化,但不会更新它们!

#2


3  

use .data()

使用.data()

var id= $('#like').data('value');

.data() Store arbitrary data associated with the matched elements

.data()存储与匹配元素关联的任意数据

#3


2  

jQuery's data() will get the data attribute (but not set it) :

jQuery的data()将获取数据属性(但不设置它):

var id = $('#like').data('value');

or plain JS

或简单的JS

var id = document.getElementById('like').getAttribute('data-value');

#4


2  

You need to use data to retrieve a data attribute:

您需要使用数据来检索数据属性:

var id = $('a[data-value]').data('value');

Or, given that the a element has an id:

或者,假设a元素具有id:

var id = $('#like').data('value');

You can use attr(), but note this will only get data attributes which are attached to the DOM element and as such is not considered good practice. Data attributes added using jQuery will only appear in the data cache and available through data() so cannot be retrieved in this way:

您可以使用attr(),但请注意,这只会获取附加到DOM元素的数据属性,因此不被视为良好实践。使用jQuery添加的数据属性只会出现在数据缓存中并可通过data()获取,因此无法以这种方式检索:

var id = $('#like').attr('data-value');

#1


3  

Plain JavaScript has 2 Variants: getAttribute() and dataset:

普通JavaScript有2个变体:getAttribute()和数据集:

var el = document.getElementById( 'like' );
el.getAttribute( 'data-value' ); 
el.dataset.value;

As you tagged this with jQuery as well, you could use data() or attr():

当你用jQuery标记它时,你可以使用data()或attr():

$( '#like' ).data( 'value' );
$( '#like' ).attr( 'data-value' );

Using data() you can only read the value, but not update it! data() refers to a jQuery data object, which is just initialized with data- fields of the HTML element, but does not update them!

使用data()只能读取值,但不能更新它! data()指的是一个jQuery数据对象,它只是用HTML元素的数据字段初始化,但不会更新它们!

#2


3  

use .data()

使用.data()

var id= $('#like').data('value');

.data() Store arbitrary data associated with the matched elements

.data()存储与匹配元素关联的任意数据

#3


2  

jQuery's data() will get the data attribute (but not set it) :

jQuery的data()将获取数据属性(但不设置它):

var id = $('#like').data('value');

or plain JS

或简单的JS

var id = document.getElementById('like').getAttribute('data-value');

#4


2  

You need to use data to retrieve a data attribute:

您需要使用数据来检索数据属性:

var id = $('a[data-value]').data('value');

Or, given that the a element has an id:

或者,假设a元素具有id:

var id = $('#like').data('value');

You can use attr(), but note this will only get data attributes which are attached to the DOM element and as such is not considered good practice. Data attributes added using jQuery will only appear in the data cache and available through data() so cannot be retrieved in this way:

您可以使用attr(),但请注意,这只会获取附加到DOM元素的数据属性,因此不被视为良好实践。使用jQuery添加的数据属性只会出现在数据缓存中并可通过data()获取,因此无法以这种方式检索:

var id = $('#like').attr('data-value');