jQuery $(this)问题,错误的对象

时间:2011-09-27 21:10:06

标签: jquery plugins

我使用此代码显示标题

$(".line").mopTip({
'w':150, 
'style':"overOut", 
'get': $(this).attr("title")
});

但作为标题,我得到了页面的标题......我错了什么?

3 个答案:

答案 0 :(得分:8)

您在文档窗口范围内使用this,这就是它返回文档标题的原因。要获得每一行的标题,你必须遍历每一行。

$(".line").each(function(){
    //inside the each the scope of this refers to the current line
    $(this).mopTip({
        'w':150, 
        'style':"overOut", 
        'get': $(this).attr("title")
    });
});

答案 1 :(得分:0)

$(this).attr("title")

这将获取页面的属性“title”,它在文档中调用时始终会执行。

如果类“行”是目标,请将$("this")替换为$(".line")

答案 2 :(得分:0)

对于设置内容' mopTip ',请使用构造

... = $( setting.get ).html();

'get' - 初始化参数。

您将代码转换为

$( "Title of page" ).html();

换句话说,在params中,'get'必须是选择器...或返回选择器的函数。正确的HTML页面架构。或者正确的jQuery-plugin代码。或者将init包装到each()。