[英]Get the data-target dynamically
I have the follwing link: 我有以下链接:
<a href="edit/2" class="popup" data-toggle="modal" data-target="#productVariantModal"> <span class="glyphicon glyphicon-edit"></span></a>
I would like to get the data-target dynamically so I could reuse my javescript code. 我想动态获取数据目标,以便可以重用我的javescript代码。
I have tried something like this: 我已经尝试过这样的事情:
var target = $(this).closest('[data-target]');
But this does't work. 但这行不通。
Any Suggestions are welcome. 欢迎任何建议。
Thanks. 谢谢。
You need #productVariantModal
right ? 您需要
#productVariantModal
对吗? So.Simple use attr
So.Simple使用
attr
var getValue = $('.popup').attr("data-target");
if it dynamically 如果它是动态的
var getValue = $(this).attr("data-target");
var target = $(this).closest('[data-target]');
应该是这样的:
var target =$('.popup').data('target');
假设您在该超链接触发的某个事件的回调中使用此方法,则可以获取数据的值:
$(this).data('target')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.