html5(数据和属性不同)不是工作数据更新吗?

时间:2018-09-19 23:44:31

标签: jquery html css html5 frontend

我要设置“ data-hello” 2的值

 $('# Indexdiv').data('hello', 2); // not working, why?   
 $('#indexDiv').attr('data-hello',2); //is working. 

为什么data(key, value)不起作用?

enter image description here

    <!DOCTYPE html>
    <html>
    <head>
        <title>Page Title</title>
    
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    </head>
    <body>
        <div id="indexDiv" data-hello=1>
            Hello world
        </div>
        <button id="btnClick">change id attribute</button>
    </body>
    
    
    <script>
        $(function() {
            $('#btnClick').click(function(){
                $('#indexDiv').data('hello',2);  //not working, why???
               // $('#indexDiv').attr('data-hello',2); //is working
            });
         });
    </script>
    </html>

0 个答案:

没有答案