[英]How can I get the value of data attribute from a function in WordPress and put it inside a variable in jquery?
I have a WordPress function that overrides a native WordPress Gallery. 我有一个WordPress功能可以覆盖本机WordPress画廊。 I added the tooltipster.js on that function, but I want to get the value of the data-image attribute to get the url and then put it in the tooltipster initialization. 我在该函数上添加了tooltipster.js ,但我想获取data-image属性的值以获取url,然后将其放入tooltipster初始化中。
With my code, the tooltip works, but it is not getting the image url. 使用我的代码,工具提示可以正常工作,但无法获取图片网址。
I have a WordPress code that is inside functions.php like this: 我在functions.php内有一个WordPress代码,如下所示:
<li> <a class="tooltip" data-image="<?php echo $attachment_img_url; ?>"> <?php echo wp_get_attachment_image( $attachment->ID, 'thumbnail' ); ?></a> </li>
and the tooltipster initialization that's inside footer.php: 和footer.php中的工具提示初始化:
$(document).ready(function() {
var imgUrl = $(this).data('image');
$('.tooltip').tooltipster({
content: $('<span><img src="' + imgUrl + '" /></span>')
});
});
According to the tooltipster documentation you can use the method functionBefore
to run a function before the tooltip displays. 根据工具提示文档,可以使用方法functionBefore
运行工具提示显示之前的函数。 The first parameter of this function is origin
or the element that was hovered over to open the tooltip. 该函数的第一个参数是origin
或悬停在其上以打开工具提示的元素。 Knowing this you can use origin.data('image')
to get the data of data-image
and set it to the content of tooltip with origin.tooltipster('content', origin.data('image'));
知道了这一点,您可以使用origin.data('image')
来获取数据data-image
的数据,并通过origin.tooltipster('content', origin.data('image'));
将其设置为工具提示origin.tooltipster('content', origin.data('image'));
. 。
$('.tooltip').tooltipster({
content: 'Loading...',
functionBefore: function(origin, continueTooltip) {
continueTooltip();
origin.tooltipster('content', origin.data('image'));
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.