如何使用jQuery/JS从DIV获取数据属性并将其写入另一个DIV



需要从被单击的div中获取数据属性值,并将其作为数据属性传递给显示新div:

点击选择div:

<div class="mySlot" data-slot-label="a">A</div>
<div class="mySlot" data-slot-label="b">B</div>
<div class="mySlot" data-slot-label="c">C</div>

将其上的数据槽值作为其attr的弹出式DIV:

<div class="selection" data-slot-label="[The Selected DIV data]"></div>

我正在使用这个,但不工作:

$('.mySlot').click(function() {
var mydata = $('.mySlot').data('slot-label');
$('.selection').data('slot-label',mydata);
});

欢呼
$('.mySlot').click(function() {
var mydata = $(this).attr('data-slot-label');
$('.selection').attr('data-slot-label',mydata);
});

仅使用attr函数从元素中添加或检索数据

最新更新