jquery获得一个自定义(编造的)属性值

时间:2022-11-27 08:03:23

I have a the following html piece :

我有一个如下的html片段:

<div onClick="javascript:getComments(this);" store-id="143568" class="CountryRow" style="padding: 4px;"><div class="flag flag_az"></div> &nbsp; Azerbaijan</div>

and I would like to create a jquery function to get the value of store-id. I have the following however its not working :

我想创建一个jquery函数来获取存储id的值。我有以下几点不工作:

 getComments = function(input) {
   fValue = $(input).val( $(input).attr("store-id") );
   alert('the ID :'+fValue);
 }

can someone be kind enough to tell me what it is that I am doing wrong.

有人能好心地告诉我我做错了什么吗?

3 个解决方案

#1


7  

This works perfectly:

这是完美的:

getComments = function(input) {
  fValue =  $(input).attr("store-id");
  alert('the ID :'+fValue);
}

http://jsfiddle.net/mHBuE/

http://jsfiddle.net/mHBuE/

#2


4  

Take a look at jQuery custom selectors. Personally, I would use HTML5 data attributes for cases such as this which is already supported in jQuery.

看看jQuery自定义选择器。就我个人而言,我将使用HTML5数据属性来处理这样的情况,这在jQuery中已经得到了支持。

For whatever it's worth, considering the parameter I believe what are you trying to originally perform should be done like

无论如何,考虑到参数,我认为您最初要执行的操作应该是这样的

getComments = function(input) {
   fValue =     $(input).html( $(input).attr("store-id") );
   alert('the ID :'+fValue.html());

   }

#3


3  

all you have to do is :

你所要做的就是:

fValue = $(input).attr("store-id");

your snippet is trying to add to the 'value' attribute of a div (which does not exist)

您的代码片段试图添加到div的“value”属性(它不存在)

#1


7  

This works perfectly:

这是完美的:

getComments = function(input) {
  fValue =  $(input).attr("store-id");
  alert('the ID :'+fValue);
}

http://jsfiddle.net/mHBuE/

http://jsfiddle.net/mHBuE/

#2


4  

Take a look at jQuery custom selectors. Personally, I would use HTML5 data attributes for cases such as this which is already supported in jQuery.

看看jQuery自定义选择器。就我个人而言,我将使用HTML5数据属性来处理这样的情况,这在jQuery中已经得到了支持。

For whatever it's worth, considering the parameter I believe what are you trying to originally perform should be done like

无论如何,考虑到参数,我认为您最初要执行的操作应该是这样的

getComments = function(input) {
   fValue =     $(input).html( $(input).attr("store-id") );
   alert('the ID :'+fValue.html());

   }

#3


3  

all you have to do is :

你所要做的就是:

fValue = $(input).attr("store-id");

your snippet is trying to add to the 'value' attribute of a div (which does not exist)

您的代码片段试图添加到div的“value”属性(它不存在)