最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Use jQuery.data to get a boolean from a dom element? - Stack Overflow

programmeradmin0浏览0评论

According to jQuery's .data() documentation you can use the .data() method to data prefixed attributes from a dom element. For example:

{# Include jquery.... #}
<div id='mydiv' data-foo='bar'></div>
<script>
    var foo = $('#mydiv');
    foo.data('foo');   // == 'bar'
</script>

That said, I'm curious how you set and pass boolean values in these dom objects. As far as I know, this does not work:

Throws a javascript error:

{# Include jquery.... #}
<div id='mydiv' data-foo=false></div>

Sets a string instead of a boolean:

{# Include jquery.... #}
<div id='mydiv' data-foo='false'></div>
<script>
    var foo = $('#mydiv');
    foo.data('foo');   // == 'false'
</script>

So, how do I set boolean values in the dom? Or, do I have to convert these string values to booleans in my javascript (which seems lame)?

According to jQuery's .data() documentation you can use the .data() method to data prefixed attributes from a dom element. For example:

{# Include jquery.... #}
<div id='mydiv' data-foo='bar'></div>
<script>
    var foo = $('#mydiv');
    foo.data('foo');   // == 'bar'
</script>

That said, I'm curious how you set and pass boolean values in these dom objects. As far as I know, this does not work:

Throws a javascript error:

{# Include jquery.... #}
<div id='mydiv' data-foo=false></div>

Sets a string instead of a boolean:

{# Include jquery.... #}
<div id='mydiv' data-foo='false'></div>
<script>
    var foo = $('#mydiv');
    foo.data('foo');   // == 'false'
</script>

So, how do I set boolean values in the dom? Or, do I have to convert these string values to booleans in my javascript (which seems lame)?

Share Improve this question asked Jun 7, 2012 at 2:55 coonceseancooncesean 1,2942 gold badges19 silver badges27 bronze badges 1
  • 1 jsfiddle/zerkms/pnsfL - here I get the boolean – zerkms Commented Jun 7, 2012 at 2:59
Add a ment  | 

4 Answers 4

Reset to default 3

Your example just works :-)

http://jsfiddle/zerkms/pnsfL/

https://github./jquery/jquery/blob/master/src/data.js#L335

data = data === "true" ? true :
data === "false" ? false :
data === "null" ? null :
jQuery.isNumeric( data ) ? parseFloat( data ) :
    rbrace.test( data ) ? jQuery.parseJSON( data ) :
    data;

So jquery guesses the boolean, null, numeric types and JSON (!!! that's new for me)

You can simply use type-conversion. data-foo could be set to 1 = true or 0 = false and use this example: http://jsfiddle/QAkQW/

If you want boolean value, you could just do it by Set or Not Set this attribute.

But it seems that jQuery.data has already get boolean value for you.

Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null) otherwise it is left as a string.

You should simply be able to do something like this: (which is what you are doing)

<div data-role="page" data-last-value="43" data-hidden="true" data-options='{"name":"John"}'></div>

$("div").data("hidden") === true;

Look at the example code for .data().

发布评论

评论列表(0)

  1. 暂无评论