Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript html5数据属性的用例_Javascript_Jquery_Html - Fatal编程技术网

Javascript html5数据属性的用例

Javascript html5数据属性的用例,javascript,jquery,html,Javascript,Jquery,Html,我在使用数据属性时遇到问题。当我使用data(key,value)添加数据或使用removeData删除数据时,它不会向html标记添加/删除数据属性;我猜它只在内部javascript中执行,而不是通过标记 这个问题还引出了另一个问题,因为我对使用数据属性不是很有经验。数据属性的用例是什么?下面的代码是它们的“合法”用法吗 <!--- This code is supposed to animate between the sections when button is clic

我在使用数据属性时遇到问题。当我使用
data(key,value)
添加数据或使用
removeData
删除数据时,它不会向html标记添加/删除数据属性;我猜它只在内部javascript中执行,而不是通过标记

这个问题还引出了另一个问题,因为我对使用数据属性不是很有经验。数据属性的用例是什么?下面的代码是它们的“合法”用法吗

<!---
    This code is supposed to animate between the sections when button is clicked.
    But it doesn't animate from second to third but just appends the next section (so, it becomes contacts+photo, instead of just contacts) because the data attribute is not added to the next section on the first click. I will try to create a fiddle and upload it to show a live result.

-->
<div data-ext="inf-slide">
    <section data-step="demographics" data-current="1">
        <!-- Content here -->
        <button data-next="contacts">
    </section>

    <section data-step="contacts">
        <!-- Content here -->
        <button data-next="photo">
    </section>

    <section data-step="photo">
        <!-- Content Here -->
        <button data-submit="/submit.php"> <!-- Ajax submit -->
    </section>
</div>

$(document).ready(function() {
    $('[data-ext="inf-slide"]').find('*[data-step]').hide();
    $('[data-ext="inf-slide"]').find('*[data-current]').show();
    $('[data-ext="inf-slide"]').find('*[data-next]').click(function(e) {
        e.preventDefault();
        $next = $('[data-step='+$(this).data('next')+']');
        $current = $('*[data-current]').removeData('current');
        $next.data('current','1');
        $next.slideDown('fast');
        $current.slideUp('fast');
    });
});

$(文档).ready(函数(){
$('[data ext=“inf slide”]')。查找('*[data step]')。隐藏();
$('[data ext=“inf slide”]')。查找('*[data current]')。显示();
$('[data ext=“inf slide”]')。查找('*[data next]')。单击(函数(e){
e、 预防默认值();
$next=$('[data step='+$(this.data('next')+']);
$current=$('*[data current]')。removeData('current');
$next.data('current','1');
$next.slideDown('fast');
$current.slideUp('fast');
});
});
[编辑]Fiddle:

。数据(键、值)
将存储与匹配元素关联的任意数据,它不会向选定元素添加或更新数据-*属性

如果需要设置实际的HTML数据-*属性,则需要使用以下选项:

$(this).attr("data-class_value", "new value");
您的用例是正确的,您可以使用HTML5数据属性来存储任何数据