我要设置“ data-hello” 2的值
$('# Indexdiv').data('hello', 2); // not working, why?
$('#indexDiv').attr('data-hello',2); //is working.
为什么data(key, value)
不起作用?
<!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>