开发者

taking text from title attribute and adding it into a div in jquery

开发者 https://www.devze.com 2023-01-01 17:13 出处:网络
Basically i\'m trying to grap the text from the title a开发者_开发技巧ttribute in a link and show it in a paragaph in another div when a user hovers of the item, and then hide it when the user moves a

Basically i'm trying to grap the text from the title a开发者_开发技巧ttribute in a link and show it in a paragaph in another div when a user hovers of the item, and then hide it when the user moves away. so far what i've got is

(function($) {
    $.fn.showText = function() {
        $(this).hover(
            function() {
                $(this)
                    .data('title', $(this).attr('title'))
                    .removeAttr('title')
        .appendTo(".itemDecription p");
            }, 
            function() {
                $(this).attr('title', $("this").data('title'));
            }
        );
    }
})(jQuery);

//call the plugin
$(function(){
$('.wrapper ul li a').showText();

}); 

I got the code from somewhere else on stackoverflow, but i just can't seem to get it to work, and help would be much appriciated.


I am not sure about the data stuff and why you want to remove the title attribute from the element (I see no reason to do it) but you probably want:

(function($) {
    $.fn.showText = function() {
        this.each(function() {
            $(this).hover(
               function() {        
                   $(".itemDecription p").text($(this).attr('title'));
                   $(this).data('title', $(this).attr('title')).removeAttr('title');
               }, 
               function() {
                   $(".itemDecription p").empty();
                   $(this).attr('title', $(this).data('title'));
               }
            );
        });
        return this;
    }
})(jQuery);

It is important that you use each() because the selector might select multiple a elements (maybe this is reason why it does not work).

So also How to develop a jQuery plugin


.appendTo("itemDecription p"); // error here... it's either '.itemDecription p' or '#itemDecription p'

edit

I modified your codes... please look...

0

精彩评论

暂无评论...
验证码 换一张
取 消