0

我有许多图像的网格,每个图像都有一个 CSS 类设置为它的唯一 ID。

使用此 javascript 代码,我想要求我的服务器后端为所述元素构建适当的 HTML 摘要并将其返回。

$('.tier-items a img').qtip({
    content: {
        text: 'Loading...', // The text to use whilst the AJAX request is loading
        ajax: {
            url: '/Items/GetToolTip/' + $(this).attr("class"), //I assumed that $(this) would contain the element I moused over.
            type: 'GET', // POST or GET
            data: {} // Data to pass along with your request
        }
    },
    style: {
        classes: 'ui-tooltip-youtube ui-tooltip-shadow'
    },
    position: {
        my: 'bottom center',
        at: 'top center',
        effect: function (api, pos, viewport) {
            // "this" refers to the tooltip
            $(this).animate(pos, {
                duration: 600,
                easing: 'linear',
                queue: false // Set this to false so it doesn't interfere with the show/hide animations
            });
        }
    }
});

但是,在这种情况下,$(this)似乎不包含已被鼠标悬停以调用 qTip 的元素。

我需要调用一个像这样的 url:

/Items/GetToolTip/23

相反,我得到:

/Items/GetToolTip/undefined?_=1334977600119

有什么方法可以获取调用 QTip 的元素吗?


试过这个,仍然没有骰子:

itemHoverId = 1;

//Construct the id, on mouseenter and use that instead of $(this)
$('.tier-items a img').mouseenter(function () {
    itemHoverId = $(this).attr("class");
    alert(itemHoverId);
});

$('.tier-items a img').qtip({
    content: {
        text: 'Loading...', // The text to use whilst the AJAX request is loading
        ajax: {
            url: '/Items/GetToolTip/' + itemHoverId, //I assumed that $(this) would contain the element I moused over.
            type: 'GET', // POST or GET
            data: {} // Data to pass along with your request
        }
    },

结果调用是:

/Items/GetToolTip/1?_=1334978818620
4

1 回答 1

2

问题是thisthis的是调用初始化方法的时间而不是您的图像(即可能您的文档已准备好)如果您需要每个 img 的单独 url,您可以单独创建一个 qtip 小部件

$('.tier-items a img').each(function() { $(this).qtip({
content: {
    text: 'Loading...', // The text to use whilst the AJAX request is loading
    ajax: {
        url: '/Items/GetToolTip/' + $(this).attr("class"), //I assumed that $(this) would contain the element I moused over.
        type: 'GET', // POST or GET
        data: {} // Data to pass along with your request
    }
}, ....
于 2012-04-21T03:26:30.660 回答