是否可以根据数据属性组装整个函数?我正在尝试组装大量数据值。是否有一些我缺少的代码片段,或者用javascript语言认为这是不可能的。
$(".div")'.'+$(this).data('event')(function () {
$(this).data('window-method')('I was '+$(this).data('event')+'ed');
$(this)'.'+$(this).data('method')($(this).data('property'), $(this).data('value'));
});
// It should say something along these lines
// $(".div").click(function () {
// alert('I was clicked');
// $(this).css('height', '20');
.div {
width: 100px;
height: 100px;
transition: 1s;
float: left;
margin: 10px;
background: teal;}
<script
src="https://code.jquery.com/jquery-1.11.0.js"
integrity="sha256-zgND4db0iXaO7v4CLBIYHGoIIudWI5hRMQrPB20j0Qw="
crossorigin="anonymous"></script>
<div
data-event="click"
data-window-method="alert"
data-method="css"
data-property="height"
data-value="200"
class="div"></div>
<div
data-event="dblclick"
data-window-method="alert"
data-method="css"
data-property="width"
data-value="150"
class="div"></div>
<div
data-event="hover"
data-window-method="alert"
data-method="css"
data-property="padding"
data-value="40"
class="div"></div>