Is there a way to add a tooltip to every class of myClass on the page?

I have many divs with the same class, like so:

<div class="myClass">Div 1</div>
<div class="myClass">Div 2</div>
<div class="myClass">Div 3</div>
<div class="myClass">Div 4</div>

I need to give them all the same tooltip.

For example, pseudo code:

$(".myClass").tooltip({
    text: 'Tooltip Text'
});

Sorry if this has been asked before, couldn't seem to find it anywhere.

有帮助吗?

解决方案

you mean to all classes that found on a page ?

<div class="myClass" title="Hello">...</div>
<div class="myClass" title="Hello">...</div>
<div class="myClass" title="Hello">...</div>
<span class="myClass" title="Hello">...</span>
<p class="myClass" title="Hello">.....</p>

or just any element with class '.myClass' ?

jquery tooltip takes all element with title

$(function() {
   $( ".myClass" ).tooltip();
 });

or this

  $(function() {

    $( ".myClass" ).tooltip({content:function(){

       return "My title"
     }
    });
 });

it will result this : try it

其他提示

Jquery tooltip uses the title attribute. You need something like

$('.myClass').attr('title', 'my tooltip');
$('.myClass').tooltip();

Fiddle here

http://jsfiddle.net/sfarsaci/86DmG/

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top