简体   繁体   English

如何从WordPress中的函数获取data属性的值并将其放在jquery中的变量中?

[英]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'));
    }
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从内联样式属性中获取值并将其放入最接近的输入值中? jQuery的 - How can I get a value from an inline style attribute and put inside the closest input value? jQuery 如何将函数内部变量的值获取到 on jquery 外部 - how can i get the value of variable inside function to outside the on jquery 如何从元素中获取 jQuery 中数据属性的值? - How can I get the value of data attribute in jQuery from an element? jQuery数据属性作为选择器:如何从onclick函数中的属性获取值? - jquery data attribute as selector: how to get value from attribute inside onclick function? 如何从内部获取价值 <li> 元素并使用jQuery将其放入图像属性标记 - How to get value from inside <li> element and put it to image attribute tag using jQuery 如何从php变量中获取数据属性? - How can I get the data attribute from a php variable? 如何获取两个属性的值并将其放在另一个属性中? - How can I get the value of two attributes and put it in another attribute? 我如何使用jQuery在双引号内给出数据属性变量 - How can i give data attribute variable inside the double quotes using Jquery 如何从函数jquery中获取变量 - how can I get a variable from a function jquery 如何从CodeIgniter存储的变量中获取JQuery中的值 - How can I get value in JQuery from variable stored with CodeIgniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM