te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Native JS for Reading HTML5 Custom Data Attributes - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Native JS for Reading HTML5 Custom Data Attributes - Stack Overflow

programmeradmin5浏览0评论

I have learned that HTML5 includes a means to set custom attributes on elements using the data- prefix. However I'm a bit scewered in terms of how to read the properties during a javascript code block. I guess it is my interpretation of how the DOMStringMap is working thats off.

Could someone simplify how to read the properties of the following sample html.

<span data-plex-key="howtoRead" data-id="anId">inner</span>

Trying following doesnt really work as expected

spanEl.dataset['id']                    // straight-forward and result is anId
spanEl.dataset['plex-key']           // undefined
spanEl.dataset['plex']['key']        // throws 'cannot read property of undefined'
spanEl.getAttribute('plex-key')      // there's a null however,
spanEl.getAttribute('data-plex-key') // this variant seems to work

Another thing that makes me wonder is, the CSS selectors seems to follow the excact same pattern as to which is i written in the DOM, so why is this not the case with reading from javascript.

For instance, this would match

 span[data-plex-key="howtoRead"] { color:green }

Appreciate the help, still getting more and more intreaged with the HTML5 Canvas, Video and local Data Storage :)

I have learned that HTML5 includes a means to set custom attributes on elements using the data- prefix. However I'm a bit scewered in terms of how to read the properties during a javascript code block. I guess it is my interpretation of how the DOMStringMap is working thats off.

Could someone simplify how to read the properties of the following sample html.

<span data-plex-key="howtoRead" data-id="anId">inner</span>

Trying following doesnt really work as expected

spanEl.dataset['id']                    // straight-forward and result is anId
spanEl.dataset['plex-key']           // undefined
spanEl.dataset['plex']['key']        // throws 'cannot read property of undefined'
spanEl.getAttribute('plex-key')      // there's a null however,
spanEl.getAttribute('data-plex-key') // this variant seems to work

Another thing that makes me wonder is, the CSS selectors seems to follow the excact same pattern as to which is i written in the DOM, so why is this not the case with reading from javascript.

For instance, this would match

 span[data-plex-key="howtoRead"] { color:green }

Appreciate the help, still getting more and more intreaged with the HTML5 Canvas, Video and local Data Storage :)

Share Improve this question edited Jan 23, 2015 at 14:24 juan.facorro 9,9302 gold badges35 silver badges41 bronze badges asked Sep 26, 2012 at 16:34 mschrmschr 8,6413 gold badges23 silver badges35 bronze badges 1
  • You shouldn't use dashes in the attribute keys BTW, better opt for camelCased plexKey here. – m90 Commented Sep 26, 2012 at 16:48
Add a ment  | 

4 Answers 4

Reset to default 11

In vanilla-JS, assuming spanEl is a reference to the DOM node

spanEl.dataset.plexKey

will work using the camelCase notation (see http://jsbin./oduguw/3/edit) when your data attribute contains hypens (-) and also

spanEl.getAttribute('data-plex-key')

will work fine as you already noticed. As a side note, in jQuery you can access to that data attribute with

$(spanEl).data("plex-key")

In Chrome, it seems to map the data keys in a not-so-straightforward way:

console.log(spanEl.dataset);​​​​​​​​​​​​​​
//shows:
//DOMStringMap
//  plexKey: "howtoRead"
//  id: "anId"

It converts "plex-key" to "plexKey".

While not being pletely straightforward, this behavior is defined in the HTML5 spec here:

http://dev.w3/html5/spec//global-attributes.html#dom-dataset

Your first and last method are correct while not using any libraries. However a key with a minus sign is converted to Camel Case, so plex-key bees plexKey:

spanEl.dataset['id']
spanEl.dataset['plexKey']
spanEl.getAttribute('data-plex-key')

However, only the last one works in IE up to 9. (I don't know about 10.) The data attributes are nothing else than normal attributes having a naming convention in this case.

 spanEl.dataSet["plexKey"]   

//Using jQuery you can try this

 $('span').data('plex-key')  // Will give you **howtoRead**

    $('span').data('id')  // Will give you **anId**
发布评论

评论列表(0)

  1. 暂无评论