不使用jQuery访问'data-'属性 [英] Access 'data-' attribute without jQuery

查看:75
本文介绍了不使用jQuery访问'data-'属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在没有jQuery的情况下访问数据属性吗?

Can I access a data- attribute without jQuery?

使用jQuery很容易,但是如果没有jQuery,我无法在任何地方看到如何做到这一点。

It's easy with jQuery, but I can't see anywhere how to do it without jQuery.

如果我在没有jQuery的情况下搜索Google,我得到的就是jQuery示例。

If I search on Google 'without jQuery' all I get is jQuery examples.

它甚至可能吗?

推荐答案

On 这里我找到了这个例子:

On here I found this example:

<div id='strawberry-plant' data-fruit='12'></div>
<script>
    // 'Getting' data-attributes using getAttribute
    var plant = document.getElementById('strawberry-plant');
    var fruitCount = plant.getAttribute('data-fruit'); // fruitCount = '12'
    // 'Setting' data-attributes using setAttribute
    plant.setAttribute('data-fruit', '7'); // Pesky birds
</script>

所以看起来非常可行。

这篇关于不使用jQuery访问'data-'属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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