jquery:不能获取“值”属性的div [英] jquery: cannot fetch the "value" attribute of a div

查看:104
本文介绍了jquery:不能获取“值”属性的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的chrome javascript控制台的屏幕截图,演示了我的困境。





的值属性我真的不明白为什么我无法获取value属性。
class属性工作正常,所以我的数字应该适用于值。
我在我的应用程序中测试的代码(coffeescript)如下:

  $  - > 
$(。comment)。click - >
alert $(this).attr'value'

这个pic工作。



有谁知道我做错了什么,我应该做什么?提前感谢!

解决方案

使用 .val()。这只能在 select,input,textarea 标签上工作。对于其他标签, value 属性无效。



如果使用最新的jQuery, $ c> data - attirbutes:

 < div class =commentdata-value =64> 

var value = $('[div selector]')。data('value');


Here's a screenshot of my chrome javascript console demonstrating my dilemma.

I seriously cannot understand why i can't fetch the "value" attribute. The "class" attribute works just fine, so i figure the same should apply to "value". The code (coffeescript) i'm testing in my app looks like this:

$ ->
  $(".comment").click ->
   alert $(this).attr 'value'

Neither this nor the code shown in this pic work.

Does anyone know what i'm doing wrong or what i SHOULD be doing? Thanks in advance!

解决方案

Use .val(). This will work only on select, input, textarea tags. For other tags, value attribute is not valid.

If you're using latest jQuery, use data- attirbutes instead:

<div class="comment" data-value="64">

 var value = $('[div selector]').data('value');

这篇关于jquery:不能获取“值”属性的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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