JQuery Title属性这个函数

时间:2011-03-22 12:31:20

标签: jquery title

我制作了一个脚本,该脚本应该使用链接onMouseover的标题完成div的HTML。它不起作用。

代码

function title(id) {
    var thetitle = $(id).attr('title');
    $('#top_video_title').fadeTo(100, 0.001);
    $('#top_video_title').html(thetitle, function(){
      $('#top_video_title').fadeTo(200, 1);
    });
    return false;


}

HTML

<a href="#" onmouseover="title(this);" onmouseout="titleout()" title="testing">test</a>
<a href="#" onmouseover="title(this);" onmouseout="titleout()" title="testing2">test2</a>

有什么想法吗?

非凡

3 个答案:

答案 0 :(得分:0)

this

不等于

$(this)

另见this interesting article

我认为您最好将jQuery对象作为函数的参数发送:

function title(id) {
    var thetitle = id.attr('title');
    $('#top_video_title').fadeTo(100, 0.001);
    $('#top_video_title').html(thetitle, function(){
      $('#top_video_title').fadeTo(200, 1);
    });
    return false;
}

然后在你的html中使用$(this)

<a href="#" onmouseover="title($(this));" onmouseout="titleout()" title="testing">test</a>
<a href="#" onmouseover="title($(this));" onmouseout="titleout()" title="testing2">test2</a>

答案 1 :(得分:0)

我建议改为:

$(document).ready( function() {
    /* all links with a title -- alternatively, target with classes or ids */
    $("a[title]").hover( function(e) { /* function formerly known as "title" */

        /* "this" variable in event listener is automatically
        the element the event was called on*/
        var thetitle = $(this).attr('title');

        /* duration == 100 ms
           opacity == 0.001
           callback -> called after finished fading out
        */
        $('#top_video_title').fadeTo(100, 0.001, function(){
          $('#top_video_title').html(thetitle);
          $('#top_video_title').fadeTo(200, 1);
        });
        return false;
    }
    , function(e) {/* function formerly known as "titleout", suitably modified*/
    });
})

然后是HTML:

<!-- no more inline JS :-) -->
<a href="#" title="testing">test</a>
<a href="#" title="testing2">test2</a>

答案 2 :(得分:0)

看起来JQuery不方便使用名为title()的函数。我不知道这是不是因为它用于编译API,但是一旦功能名称被更改,整个功能就完美无缺。

在我们更改功能名称之前,萤火虫报告是 - 无法找到功能。

function titlein(id) {
    var thetitle = $(id).attr('title');
    $('#top_video_title').fadeTo(50, 0.001, function() {
     $('#top_video_title').html(thetitle);
      $('#top_video_title').fadeTo(200, 1);});
    return false;
 }

非凡