2011-03-11 76 views
1

我有在.help类中追加div(帮助图标)的所有元素的函数。从追加到元素的标题属性中获取文本

jQuery().ready(function() { 
    jQuery('.help').append('<div class="mark"></div>'); 
}); 

我需要什么要补充的功能得到.help元素的标题属性信息?

我是新来的脚本。

+0

你需要获取或设置信息“标题”的每个元素的属性,以“帮助“班? – FarligOpptreden 2011-03-11 09:54:00

+0

查看jQuery API文档(http://api.jquery.com/each/)中的'each'方法。 – Lazarus 2011-03-11 09:55:11

+0

我真的不明白你的问题。你能告诉更多你的问题吗? – 2011-03-11 09:55:22

回答

2

利用的事实,append()还可以接受,这将创建HTML字符串附加功能:

$('.help').append(function() { 
    return '<div class="mark" title="' + $(this).attr('title') + '"></div>'; 
}); 
+0

+1使用函数参数 - 很好。 – johnhunter 2011-03-11 10:25:05

-1
var title = jQuery('.help').attr('title'); 

jQuery('.mark').attr('title',title); 
+0

我需要从.help中获取它并将其设置为.mark – gidzior 2011-03-11 09:55:38

+0

jQuery('。mark')。attr('title',title); – Headshota 2011-03-11 09:56:44

+0

GREAT这就是我所期待的 – gidzior 2011-03-11 10:14:26

0
var title = $('.help').attr("title"); 
-1

尝试使用attr功能:

jQuery().ready(function() { 
    console.log(jQuery('.help').attr('title')); 
}); 
1

我想这是你想要什么:

​​

});

+0

是的@johnhunter,我很高兴发布我的(第一次)答案,只是为了看到其他人也在做同样的事情,在同一分钟:) – ctekse 2011-03-11 10:12:59

1

认为您正在寻找这样的事情:

jQuery().ready(function() { 

    // with each help element.... 
    jQuery('.help').each(function() { 

     // create the empty mark, add the title from help, append mark to help 
     $('<div class="mark"></div>').attr('title', $(this).attr('title')).appendTo(this); 
    }); 
}); 

这将添加一个div.mark到每个帮助元素,并设置标志的标题是父的帮助标题。

+0

@ctekse - 重新思考同一行:) – johnhunter 2011-03-11 10:09:40

+0

伟大这是我正在寻找的':] – gidzior 2011-03-11 10:16:15