从jquery获取div值

时间:2022-01-09 20:33:09

In the following div how can i get the value=12 value when the div is clicked

在下面的div中,如何在单击div时获取value = 12的值

<div value='12' id='div1'>some content</div>

$('#div1').live("click", function(a) {
alert(this.value);
    alert($('#div1').val());  //doesnt work


});

4 个解决方案

#1


5  

use jquery attr()

使用jquery attr()

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

获取匹配元素集中第一个元素的属性值,或为每个匹配元素设置一个或多个属性。

try this

 alert($('#div1').attr('value')); 

OR

$('#div1').on("click", function(a) {

   alert($(this).attr('value'));  //doesnt work
});

#2


1  

If you can keep away from using your own HTML attributes you'll run into validation issues, a better choice would html5's data attributes. This allows you to create your own attributes good for holding this type of data. For instance:

如果你不能使用你自己的HTML属性,你会遇到验证问题,html5的数据属性会有更好的选择。这允许您创建自己的属性,以便保存此类数据。例如:

<div data-num="12" id="div1">some content</div>

    $('#div1').on('click', function(){
       alert($(this).data('num'));
    });

http://api.jquery.com/jQuery.data/

#3


0  

alert($('#div1').attr('value'));

Should do the trick. All attributes of an element can be read and set via jQuery.attr()

应该做的伎俩。可以通过jQuery.attr()读取和设置元素的所有属性

Working demo

The jQuery.live method is deprecated(v1.7) and removed(1.9), so please use jQuery.on instead as shown in the demo.

jQuery.live方法已弃用(v1.7)并已删除(1.9),因此请使用jQuery.on,如演示中所示。

#4


0  

$(document).on("click", '#div1', function(a) {

    alert(this.attributes["value"].nodeValue);
    alert(this.attributes["value"].value);
    alert(this.getAttribute("value"));

    alert($('#div1').attr("value"));

});

live() is deprecated, Use on() instead

不推荐使用live(),而是使用on()

See DEMO

#1


5  

use jquery attr()

使用jquery attr()

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

获取匹配元素集中第一个元素的属性值,或为每个匹配元素设置一个或多个属性。

try this

 alert($('#div1').attr('value')); 

OR

$('#div1').on("click", function(a) {

   alert($(this).attr('value'));  //doesnt work
});

#2


1  

If you can keep away from using your own HTML attributes you'll run into validation issues, a better choice would html5's data attributes. This allows you to create your own attributes good for holding this type of data. For instance:

如果你不能使用你自己的HTML属性,你会遇到验证问题,html5的数据属性会有更好的选择。这允许您创建自己的属性,以便保存此类数据。例如:

<div data-num="12" id="div1">some content</div>

    $('#div1').on('click', function(){
       alert($(this).data('num'));
    });

http://api.jquery.com/jQuery.data/

#3


0  

alert($('#div1').attr('value'));

Should do the trick. All attributes of an element can be read and set via jQuery.attr()

应该做的伎俩。可以通过jQuery.attr()读取和设置元素的所有属性

Working demo

The jQuery.live method is deprecated(v1.7) and removed(1.9), so please use jQuery.on instead as shown in the demo.

jQuery.live方法已弃用(v1.7)并已删除(1.9),因此请使用jQuery.on,如演示中所示。

#4


0  

$(document).on("click", '#div1', function(a) {

    alert(this.attributes["value"].nodeValue);
    alert(this.attributes["value"].value);
    alert(this.getAttribute("value"));

    alert($('#div1').attr("value"));

});

live() is deprecated, Use on() instead

不推荐使用live(),而是使用on()

See DEMO